我已經搜索並浪費了一整天的時間來嘗試所有我能想到或發現的事情來解決這個問題:我構建了我的第一個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);
[...]
您是否嘗試使用Microsoft(R)Service Configuration Editor? (工具 - > WCF配置...) – evgenyl 2013-04-07 09:37:35
我還沒有嘗試過。有趣的是,服務配置編輯器生成與我所做的完全相同的端點配置 - 合同和全部。端點仍然無法通過相同的枚舉約束違規警告消息進行驗證。 – Kenny 2013-04-07 20:20:54