2013-04-07 27 views
2

我已經搜索並浪費了一整天的時間來嘗試所有我能想到或發現的事情來解決這個問題:我構建了我的第一個WCF服務,並認爲將它託管在IIS中會很酷。這是一項長期運行的服務,所以這是一個不可行的方案。之後,我在我的解決方案中添加了另一個項目來託管WCF服務,引用了WCF項目,並且在嘗試從代碼宿主中不斷努力。 我基本上遵循了http://msdn.microsoft.com/en-us/library/ms733069.aspx的指導實施主機,對我的具體情況做了小的調整。單獨裝配WCF合同 - 如何將其作爲終端合同使用?

當前的挑戰是簡單地獲取託管項目的App.config中定義的服務端點。它無法解析我的WCF服務合約的「端點」的「合約」屬性。

WCF大會

namespace WcfFoldingService 
{ 
    [ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IFoldingUpdaterClientContract))] 
    public interface IFoldingUpdaterServiceBehavior 
    { 
     [OperationContract(IsOneWay = false, IsInitiating = true)] 
     void Subscribe(); 
     [OperationContract(IsOneWay = false, IsTerminating = true)] 
     void Unsubscribe(); 
     [OperationContract(IsOneWay = true)] 
     void PublishSomeOperation(); 
    } 
    public interface IFoldingUpdaterClientContract 
    { 
     [OperationContract(IsOneWay = true)] 
     void SomeOperation(); 
    } 
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession, ConcurrencyMode=ConcurrencyMode.Single)] 
    public class FoldingUpdaterService : IFoldingUpdaterServiceBehavior 
    { 
     // Omitted for brevity, but complete implementation exists. 
    } 
} 

/WCF大會

主持大會

App.config 
<system.serviceModel> 
<services> 
    <service name="WcfFoldingService.FoldingUpdaterService" behaviorConfiguration="httpServiceBehavior"> 
    <endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="ServiceBindingConfiguration" contract="WcfFoldingService.IFoldingUpdaterServiceBehavior"/> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 
<protocolMapping> 
    <add scheme="http" binding="wsDualHttpBinding" bindingConfiguration="ServiceBindingConfiguration"/> 
</protocolMapping> 
<bindings> 
    <wsDualHttpBinding> 
    <binding name="ServiceBindingConfiguration"> 
     <reliableSession ordered="true"/> 
     <security mode="None"/> 
    </binding> 
    </wsDualHttpBinding> 
</bindings> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="httpServiceBehavior"> 
     <serviceMetadata httpGetEnabled="True"/> 
     <serviceDebug includeExceptionDetailInFaults="False"/> 
     <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="100" maxConcurrentSessions="100"/> 
     <sendMessageChannelCache allowUnsafeCaching="false"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
</system.serviceModel> 

/託管大會

Message: 
The 'contract' attribute is invalid - The value 'WcfFoldingService.IFoldingUpdaterServiceBehavior' is invalid according to its datatype 'serviceContractType' - The Enumeration constraint failed. 

我難倒。谷歌提供了很少的相關鏈接,他們主要是處理我沒有遇到的其他配置問題。 WCF程序集被引用,我使用完全限定名稱,我甚至嘗試過使用ConfigurationName屬性來防止命名空間衝突。我是WCF新手,所以我希望這是一個明顯的問題!

編輯: 我現在使用的是編程配置而不是App.config XML。最重要的聲明是ContractDescription.GetContract(serviceType),因爲即使new ContractDescription("FullNamespace.IContract")也沒有像XML配置那樣完全失敗。 serviceType的FullName屬性的Type對象與我的「FullNamespace.IContract」完全相同。我懷疑程序集沒有被代碼中的硬引用加載的問題,但我現在還不能確定。現在,這種方法將正常工作:

using (var host = new ServiceHost(typeof(FoldingUpdaterService), baseAddress)) 
{ 
    var serviceType = typeof (IFoldingUpdaterServiceBehavior); 
    var serviceContract = ContractDescription.GetContract(serviceType); 
    var foldingEndpoint = new ServiceEndpoint(serviceContract) 
     { 
      Binding = new WSDualHttpBinding() 
      Address = new EndpointAddress(new Properties.Settings().WcfUpdaterServiceAddress) 
     }; 
    host.Description.Endpoints.Add(foldingEndpoint); 
    [...] 
+0

您是否嘗試使用Microsoft(R)Service Configuration Editor? (工具 - > WCF配置...) – evgenyl 2013-04-07 09:37:35

+0

我還沒有嘗試過。有趣的是,服務配置編輯器生成與我所做的完全相同的端點配置 - 合同和全部。端點仍然無法通過相同的枚舉約束違規警告消息進行驗證。 – Kenny 2013-04-07 20:20:54

回答

1

自託管應用程序將需要<host>標籤中<service>標籤:

<system.serviceModel> 
<services> 
    <service name="WcfFoldingService.FoldingUpdaterService" behaviorConfiguration="httpServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8080/Services/"/> 
      </baseAddresses> 
     </host> 
    <endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="ServiceBindingConfiguration" contract="WcfFoldingService.IFoldingUpdaterServiceBehavior"/> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 
+0

感謝您的輸入;我確實需要這個,但這個問題涉及到另一個問題。 – Kenny 2013-04-07 20:17:09

+0

@ Kenny:我將你的代碼複製粘貼到一個新的解決方案中。 WCF應用程序和控制檯應用程序作爲引用WCF項目的服務主機。有效!我無法重現您提到的錯誤。我使用了Visual Studio 2012. – 2013-04-07 20:35:36

+0

我也在使用2012!我不確定它爲何難以解決合同。我只是推出了一個新的控制檯應用程序主機,以嘗試編程配置而不是XML配置,我不得不使用Type方法來獲得我的合同。很奇怪......我會用我的代碼回答。感謝你在這方面的努力;我仍然不確定XML配置有什麼問題,但至少我現在有了一個角度。 – Kenny 2013-04-07 21:02:40

-1

確保您的主機制造業務應用的參考

0

我不知道這是否仍然是一個問題,但最近偶然發現了同樣的問題,可能會有所幫助。

在指定服務名稱時,還必須使用您的名稱空間。因此,可以說您的命名空間將myhost然後輸入正確的文字會...

<service name="myhost.WcfFoldingService.FoldingUpdaterService"...>

這同樣適用於合同。

3

這是一個老問題,但如果有人有同樣的問題,這對我有用: 嘗試關閉Visual Studio並刪除與解決方案文件(.sln)位於同一目錄中的.suo文件。這會重置編輯器緩存。在將服務合同文件移至其他程序集並且緩存仍然指向舊位置後,我遇到了同樣的問題。

0

我的解決方案使用VS2012創建,包含多個.NET 4.0項目。我切換到VS2013,當我添加一個新的WCF庫項目時,它是使用.NET 4.5創建的。將新項目重置爲.NET 4.0解決了我的問題。