[英]WCF service works in VS but not in build
我有一个 wcf 自托管服务,旨在供计算机和 android 手机使用。 我成功地使服务在两个目标中都可以使用,但是当我构建一个 exe 文件(负责自托管服务)时,该服务在 pc 中运行良好,但在移动应用程序上却无法运行。 这是我的服务的 app.config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<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>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="httpBinding" maxBufferSize="128000000" maxReceivedMessageSize="128000000" />
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding" closeTimeout="00:02:00" sendTimeout="00:02:00"
maxBufferSize="6553600" maxReceivedMessageSize="6553600" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<diagnostics performanceCounters="Default" />
<services>
<service name="DeltaService.Data">
<endpoint address="data" binding="basicHttpBinding" bindingConfiguration="httpBinding"
name="data" contract="DeltaService.IData">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="json" behaviorConfiguration="Rest" binding="webHttpBinding"
bindingConfiguration="webBinding" name="restdata" contract="DeltaService.IData">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/delta_api/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="Rest">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
</behavior>
<behavior name="Web">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<useRequestHeadersForMetadataAddress />
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
这是启动服务的代码:
serviceHost = new ServiceHost(typeof(Data));
NetHttpBinding netHttpBinding = new NetHttpBinding();
netHttpBinding.MaxReceivedMessageSize = 128 * 1000000; //128Mb
serviceHost.AddServiceEndpoint(typeof(IData), netHttpBinding, url);
erviceHost.Open();
您似乎是通过使用其他托管程序来托管服务的。 我们必须考虑的一件事是,在托管程序(例如 WinForms 应用程序)中托管服务时,我们不需要配置服务端点和绑定信息。 以这两种方式托管时存在区别。 当我们在Visual Studio上运行托管服务时,系统会自动搜索服务配置,这样Appconfig文件中的配置就会生效,正确绑定会生成不同种类的服务(restful和soap web服务)。 但是使用 NetHttpBinding 的服务在其他程序中托管服务时会与上述服务有所不同。
总之,我建议您在自托管程序中使用以下代码。
ServiceHost sh = new ServiceHost(typeof(Data));
sh.Open();
它将自动在 Appconfig 文件中搜索服务配置,以便以适当的方式发布服务。
如果问题仍然存在,请随时告诉我。
您已将地址配置为localhost
。 该地址只能从您的计算机访问,而不能从外部访问。
托管服务时,请选择可以从外部访问的地址。 由于我不知道您的设备在哪里,我不知道您需要多少“外部”。 本地网络? 广域网? 确保您阅读了相关教程并选择您需要的地址或 DNS 条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.