WCF 服务基地址 Http 和 netTcp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2012765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF Service Base Address Http and netTcp
提问by inutan
I have two base addresses defined in my WCF Service config file:
我在 WCF 服务配置文件中定义了两个基地址:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\WCF Service Logs\app_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="DateTime, Timestamp">
<filter type="" />
</add>
</sharedListeners>
</system.diagnostics>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcp" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000">
<readerQuotas maxDepth="500" maxStringContentLength="50000000" maxArrayLength="50000000" maxBytesPerRead="50000000" maxNameTableCharCount="50000000" />
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
name="ReportingComponentLibrary.TemplateReportService">
<endpoint address="TemplateService" binding="netTcpBinding" bindingConfiguration="netTcp"
contract="ReportingComponentLibrary.ITemplateService"></endpoint>
<endpoint address="ReportService" binding="netTcpBinding" bindingConfiguration="netTcp"
contract="ReportingComponentLibrary.IReportService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8001/TemplateReportService" />
<add baseAddress="http://localhost:8181/TemplateReportService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ReportingComponentLibrary.TemplateServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
And although I have set endpoint binding as netTcpBinding,
I am only able to access my WCF service with base address:
尽管我已将端点绑定设置为 netTcpBinding,
但我只能使用基地址访问我的 WCF 服务:
http://localhost:8181/TemplateReportService
and not with
而不是与
net.tcp://localhost:8001/TemplateReportService
How can I make my service access with netTcp address?
如何使用 netTcp 地址访问我的服务?
回答by marc_s
You defined a Net.TCP base address to be:
您将 Net.TCP 基地址定义为:
net.tcp://localhost:8001/TemplateReportService
Your endpoints with Net TCP are:
您使用 Net TCP 的端点是:
<endpoint address="TemplateService"
and
和
<endpoint address="ReportService"
So their complete service address will be "netTcp base address" + "relative address as defined on the <endpoint>element" - this gives:
因此,它们的完整服务地址将是“netTcp 基址”+“<endpoint>元素上定义的相对地址”——这给出了:
net.tcp://localhost:8001/TemplateReportService/TemplateService
and
和
net.tcp://localhost:8001/TemplateReportService/ReportService
respectfully.
尊敬。
Can you use them at these addresses??
你能在这些地址使用它们吗??
Also - you defined a "mex" (metadata exchange) endpoint for the HTTP protocol - that's why you can see something when you navigate to the HTTP address. But you did not specify a MEX endpoint for netTcp.
此外 - 您为 HTTP 协议定义了一个“mex”(元数据交换)端点 - 这就是为什么当您导航到 HTTP 地址时可以看到某些内容的原因。但是您没有为 netTcp 指定 MEX 端点。
回答by cloud120
Due to not having answers clear enough , I decided to find out by my self the best solution. Here is what you have to do to configure a WCF NET TCP service endpoint in IIS 7.0.
由于没有足够清楚的答案,我决定自己找出最佳解决方案。以下是在IIS 7.0 中配置 WCF NET TCP 服务终结点所需执行的操作。
To achieve this, we need two steps:
为了实现这一点,我们需要两个步骤:
- Configure a config file for WCF net tcp endpoint
- Configure IIS for net tcp
- 为 WCF net tcp 端点配置配置文件
- 为 net tcp 配置 IIS
1. Configure a config file for WCF net tcp endpoint
1.为WCF net tcp端点配置一个配置文件
Below is a typical config file, notice that the value for "address", in service's endpoint, is empty.
下面是一个典型的配置文件,请注意服务端点中“地址”的值为空。
Also notice that inside the node "host" Im adding 2 base addresses, an http and a net tcp protocols respectly. Since the service is hosted in IIS you don't have to worry about the absolute address in the endpoint, IIS figures this out based on the base address we have defined inside the node "host".
另请注意,在节点“主机”中,我分别添加了 2 个基地址、一个 http 和一个 net tcp 协议。由于服务托管在 IIS 中,因此您不必担心端点中的绝对地址,IIS 会根据我们在节点“主机”中定义的基地址计算出这一点。
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfig">
<!--your custom behavior here-->
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBindingConfig"/>
</basicHttpBinding>
<netTcpBinding>
<!--our netTcpBinding binding-->
<binding name="netTcpBindingConfig"/>
</netTcpBinding>
</bindings>
<services>
<service name="MyNamespace.ServiceLayer.MyService" behaviorConfiguration="behaviorConfig">
<!--Endpoint for basicHttpBinding-->
<endpoint address="" binding="basicHttpBinding" contract="MyNamespace.ServiceLayer.IMyService" bindingConfiguration="basicHttpBindingConfig"/>
<!--Endpoint for netTcpBindingConfig-->
<endpoint address="" binding="netTcpBinding" contract="MyNamespace.ServiceLayer.IMyService" bindingConfiguration="netTcpBindingConfig">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<!--Make sure you add a mexTcpBinding, without it you will receive an error when trying to resolve service's reference-->
<endpoint address="mex"
binding="mexTcpBinding"
bindingConfiguration=""
name="MyServiceMexTcpBidingEndpoint"
contract="IMetadataExchange" />
<host>
<!--This part is important for IIS to determine the base addresses for your endpoints-->
<baseAddresses>
<!--Notice that baseAddress for net tcp is preceded by net.tcp -->
<!--then ://localhost:{port} -->
<!--Also the same for http, so you can figure out how to define a baseAddress for https-->
<add baseAddress="net.tcp://localhost:8090"/>
<add baseAddress="http://localhost:8080"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
2. Configure IIS for net tcp
2.为net tcp配置IIS
In IIS, (after updated your service with new configuration) enable the net.tcp protocol. These are the steps:
在 IIS 中,(使用新配置更新您的服务后)启用 net.tcp 协议。这些是步骤:
- Open IIS.
- In sites list, find your "site" where your wcf is hosted, then right-click on it
- then, select "Edit Bindings",
- a list of bindings appears (be sure that net.tcp is no there), then add your net.tcp configuration,
- click "Add",
- Type net.tcp (or select net.tcp from a dropdown list if it is available)
- in binding Information type: 8090:* (make sure it's the same port as in host > base address you'd added)
- then close this window and restart your service.
- 打开 IIS。
- 在站点列表中,找到托管 wcf 的“站点”,然后右键单击它
- 然后,选择“编辑绑定”,
- 出现绑定列表(确保没有 net.tcp),然后添加您的 net.tcp 配置,
- 点击“添加”,
- 键入 net.tcp(或从下拉列表中选择 net.tcp,如果可用)
- 绑定信息类型:8090:*(确保它与您添加的主机>基地址中的端口相同)
- 然后关闭此窗口并重新启动您的服务。
After doing this, there is also another configuration:
这样做之后,还有一个配置:
- Open IIS
- In sites list, find your "site" where your wcf is hosted, then right-click on it
- Click "Advanced Settings"
- Select Enabled Protocols
- Set the value to http,net.tcp (be sure that there are not blanks between http,net.tcp)
- Restart your service
- 打开 IIS
- 在站点列表中,找到托管 wcf 的“站点”,然后右键单击它
- 点击“高级设置”
- 选择启用的协议
- 将值设置为http,net.tcp(确保http,net.tcp之间没有空格)
- 重启你的服务
Also, if you still cannot access service, you might need to start your Net.tcp listener adapter in your server. To achieve this, please follow next steps:
此外,如果您仍然无法访问服务,您可能需要在您的服务器中启动您的 Net.tcp 侦听器适配器。为此,请按照以下步骤操作:
- Go to ServerManager -> Configuration -> Services, stop the Net.Tcp Listener Adapter and the Net.Tcp Port Sharing Service. Then start them again.
- 转到 ServerManager -> Configuration -> Services,停止 Net.Tcp Listener Adapter 和 Net.Tcp Port Sharing Service。然后再次启动它们。
That's all you have to do to have a nettcp WCF endpoint enabled in IIS.
这就是在 IIS 中启用 nettcp WCF 端点所需要做的全部工作。
Hope this helps.
希望这可以帮助。
Regards
问候

