将WCF服务配置为从Internet访问



我是网络世界的新手,已经开发了一个服务库,我打算在Windows窗体应用程序中托管它,我需要能够通过互联网访问服务,但我无法识别问题。我已经重定向了Reuter中的端口,并禁用了防火墙,但它抛出了一个错误System。ServiceModel。EndpointNotFoundException…有人能帮我吗?

我的appconfig

<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basic_bindingSettings" maxBufferPoolSize="2147483647"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
<wsHttpBinding>
<binding name="ws_bindingSettings" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="GCService.Service1">
<endpoint address=""
binding="wsHttpBinding" bindingConfiguration="ws_bindingSettings"
name="WSHttpBinding_IService1" contract="GCService.IService1" />
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.100:28615/GCService/Service1" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>

运行服务器

Uri baseAddress = new Uri("http://192.168.1.100:28615/GCService/Service1");
ServiceHost serviceHost = new ServiceHost(typeof(GCService.Service1), baseAddress);

ip 192.168.1.100是本地pc 的ip

客户端

EndpointAddress uri = new EndpointAddress("http://fabianwesling.dynu.com:28615/GCService/Service1");
ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client("WSHttpBinding_IService1", uri);
ws.GetProducts();

首先,服务器端的serviceHost应该是这样的:

ServiceHost serviceHost = new ServiceHost(typeof(ConsoleApp10.Test));
serviceHost.Open();

因为您已经在配置文件中配置了服务的基本地址,所以无需在此处进行配置,并且必须打开服务。

根据您提供的信息,我发现您的客户端使用了一个代理类。调用的URI应该与服务端点的地址相同:

EndpointAddress uri = new EndpointAddress("http://localhost:28615/GCService/Service1");
ServiceReference1.TestClient testClient = new ServiceReference1.TestClient("WSHttpBinding_IService1", uri);

事实上,如果通过服务引用添加的代理类可以直接创建testClient,如下所示:

ServiceReference1.TestClient testClient = new ServiceReference1.TestClient();

所以你的客户的EndpointAddress应该是这样的:

EndpointAddress uri = new EndpointAddress("http://192.168.1.100:28615/GCService/Service1");

更新

如果您的WCF服务器和客户端是通过Internet连接的,请确保WCF服务器的基本地址是您在Internet中的真实IP。如果您使用的是内部网地址,如192.x.x.x,则客户端找不到该服务。

最新更新