简体   繁体   English

一个端点中的多服务合同c#wcf

[英]multi service contract in one endpoint c# wcf

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 : 我有一个名为cmsmanagement的域,但是它有多个实体。我为该域中的每个实体创建一个服务。如您在此处看到的:

在此处输入图片说明

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 . 因此,如果我的客户想要调用我的服务,则必须一一添加每个实体服务。我想为所有这些服务提供一个端点,例如,如果客户调用此mydomain / cmsmanagementservice.svc,则将调用所有这些服务。

Here is my webconfig : 这是我的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. 您可以在项目中启用WS-Discovery功能。 More here: https://msdn.microsoft.com/en-us/us-us/library/dd456791(v=vs.110).aspx 此处更多内容: https : //msdn.microsoft.com/zh-cn/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. 然后,在添加引用时,您应该能够发现具有此类行为的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM