[英]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.