2010-07-22 185 views
0

我作为Windows服务托管WCF服务。WCF多点服务端点

下面的快照。

myHost = new ServiceHost(typeof(AnalyticsService)); 
Uri address = new Uri("http://localhost:8080/MathServiceLibrary"); 

WSHttpBinding binding = new WSHttpBinding(); 
binding.MaxReceivedMessageSize = 2147483647; 
binding.MaxBufferPoolSize = 2147483647; 

//binding.Security.Mode = SecurityMode.None; 
Type contract = typeof(IAnalyticsService); 

myHost.AddServiceEndpoint(contract,binding,address); 

正如你所看到的,我以前一直只在本地公开服务。我想添加另一个ServiceEndpoint,以便我的网络上的其他机器也可以调用该服务。

我想我需要这样的添加了一些上面的代码:

myHost = new ServiceHost(typeof(AnalyticsService)); 

Uri address = new Uri("http://localhost:8080/MathServiceLibrary"); 
Uri new address = new Uri("http://xxx.xxx.xxx:8080/MathServiceLibrary"); 

WSHttpBinding binding = new WSHttpBinding(); 
binding.MaxReceivedMessageSize = 2147483647; 
binding.MaxBufferPoolSize = 2147483647; 

Type contract = typeof(IAnalyticsService); 

myHost.AddServiceEndpoint(contract, binding, address); 
myHost.AddServiceEndpoint(contract, binding, newaddress); 

现在我目前的服务库应用程序配置的样子:

<system.serviceModel> 
    <services> 
     <service behaviorConfiguration="ServiceLibrary.Service1Behavior" 
     name="ServiceLibrary.AnalyticsService"> 
     <endpoint address="" binding="wsHttpBinding" contract="ServiceLibrary.IAnalyticsService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8731/Design_Time_Addresses/ServiceLibrary/Service1/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceLibrary.Service1Behavior"> 
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="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> 
    </system.serviceModel> 

尽管服务库在我的app.config主机是

<system.serviceModel> 
    <services> 
     <service name="ServiceLibrary.AnalyticsService" 
       behaviorConfiguration ="MathServiceMEXBehavior"> 
     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8080/MathService"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="MathServiceMEXBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

我想我需要添加另一个基地址与外部(非本地主机)地址的服务库文件。我很困惑在服务库文件中改变什么。

+0

如果您发布代码或XML,请**在文本编辑器中突出显示这些行然后点击编辑器工具栏上的“代码”按钮(101 010),以良好地格式化和语法突出显示它! – 2010-07-22 18:23:49

回答

0

不,你不能添加第二个http基地址 - 你只能选择一个协议 - 一个用于http,一个用于net.tcp等。

你需要做的是建立在你的app.config第二端点为您服务主机应用程序(从服务库中的app.config甚至不会被使用 - 忘了那个文件),并为其分配一个完全合格的地址。

<system.serviceModel> 
    <services> 
     <service name="ServiceLibrary.AnalyticsService" 
       behaviorConfiguration ="MathServiceMEXBehavior"> 

     <endpoint name="firstEndpoint" 
        address="" 
        binding="wsHttpBinding" 
        contract="IAnalyticsService" /> 

     <endpoint name="secondEndpoint" 
        address="http://xxx.xxx.xxx:8080/MathServiceLibrary" 
        binding="wsHttpBinding" 
        contract="IAnalyticsService" /> 

     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8080/MathService"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    </system.serviceModel> 

所以firstEndpoint将使用基址和你能够同时secondEndpoint使用特定的,完全合格的URL来调用它下

http://localhost:8080/MathService 

而且你可以调用它在

http://xxx.xxx.xxx:8080/MathServiceLibrary 

但是:为什么你想要有第二个端点?如果您想公开不同的协议,通常只有第二个端点。你通常会有一个http,一个https,一个net.tcp端点等等。拥有两个单独的http端点以及相同的合约和相同的绑定对我来说并没有多大意义......

+0

我应该更清楚。我真正想做的就是将本地主机改为IP,并将服务提供给其他机器,并且能够从本地机器上的客户机调用服务。 – bearrito 2010-07-22 18:45:52