一个端点中的多服务合同 c# wcf [英] multi service contract in one endpoint c# wcf

查看:24
本文介绍了一个端点中的多服务合同 c# wcf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 cmsmanagement 的域,但它有多个实体.我为该域中的每个实体创建了一个服务.正如您在此处看到的:

因此,如果我的客户想要调用我的服务,他们必须一一添加每个实体服务.我希望所有这些服务都有一个端点,例如,如果客户端调用此 mydomain/cmsmanagementservice.svc.所有这些服务被称为.

这是我的网络配置:

<预><代码><配置><应用设置><add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/></appSettings><system.web><编译调试="true" targetFramework="4.5.2"/><httpRuntime targetFramework="4.5.2"/></system.web><system.serviceModel><行为><服务行为><行为><!-- 为避免泄露元数据信息,请在部署前将以下值设置为 false --><serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/><!-- 要接收故障中的异常详细信息以进行调试,请将以下值设置为 true.在部署前设置为 false 以避免泄露异常信息 --><serviceDebug includeExceptionDetailInFaults="false"/></行为></serviceBehaviors></行为><协议映射><add binding="basicHttpsBinding" scheme="https"/></protocolMapping><serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/></system.serviceModel><system.webServer><modules runAllManagedModulesForAllRequests="true"/><!--要在调试期间浏览 Web 应用程序根目录,请将以下值设置为 true.在部署前设置为 false 以避免泄露 Web 应用程序文件夹信息.--><directoryBrowse enabled="true"/></system.webServer></配置>

解决方案

您可以在您的项目中启用 WS-Discovery 功能.更多信息:https://msdn.microsoft.com/en-us/us-us/library/dd456791(v=vs.110).aspx

简而言之,它可以像这样以编程方式完成:

Uri baseAddress = new Uri(string.Format("http://{0}:8000/discovery/scenarios/calculatorservice/{1}/",System.Net.Dns.GetHostName(), Guid.NewGuid().ToString()));//为 CalculatorService 类型创建一个 ServiceHost.使用 (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress)){//添加计算器端点serviceHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), string.Empty);//** 发现 **////通过添加发现行为使服务可被发现ServiceDiscoveryBehavior discoveryBehavior = new ServiceDiscoveryBehavior();serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());//在 UDP 多播传输上发送通知discoveryBehavior.AnnouncementEndpoints.Add(新的 UdpAnnouncementEndpoint());//** 发现 **////添加指定发布服务位置的发现端点serviceHost.Description.Endpoints.Add(new UdpDiscoveryEndpoint());//打开 ServiceHost 以创建监听器并开始监听消息.serviceHost.Open();//现在可以访问该服务.Console.WriteLine("按  终止服务.");Console.ReadLine();}

然后,在添加引用时,您应该能够发现具有此类行为的服务.

I have a domain called cmsmanagement but it has several entity .I create a service for each entity in this domain .as you can see here :

So if my clients want to call my service ,they have to add each entity service one by one .I want to have one endpoint for all these service ,For example if the client call this mydomain/cmsmanagementservice.svc .all these services are called .

Here is my webconfig :

<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

解决方案

You can enable WS-Discovery feature in your project. More here: https://msdn.microsoft.com/en-us/us-us/library/dd456791(v=vs.110).aspx

In short, it can be done programmaticaly like this:

Uri baseAddress = new Uri(string.Format("http://{0}:8000/discovery/scenarios/calculatorservice/{1}/",  
        System.Net.Dns.GetHostName(), Guid.NewGuid().ToString()));  

// Create a ServiceHost for the CalculatorService type.  
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))  
{  
    // add calculator endpoint  
    serviceHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), string.Empty);  

    // ** DISCOVERY ** //  
    // make the service discoverable by adding the discovery behavior  
    ServiceDiscoveryBehavior discoveryBehavior = new ServiceDiscoveryBehavior();  
    serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());  

    // send announcements on UDP multicast transport  
    discoveryBehavior.AnnouncementEndpoints.Add(  
      new UdpAnnouncementEndpoint());  

    // ** DISCOVERY ** //  
    // add the discovery endpoint that specifies where to publish the services  
    serviceHost.Description.Endpoints.Add(new UdpDiscoveryEndpoint());  

    // Open the ServiceHost to create listeners and start listening for messages.  
    serviceHost.Open();  

    // The service can now be accessed.  
    Console.WriteLine("Press <ENTER> to terminate service.");  
    Console.ReadLine();  
}  

Then, when adding reference, you should be able to discover your services with such behaviors.

这篇关于一个端点中的多服务合同 c# wcf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆