WCF合同在单独的程序 - 如何使用它作为一个端点合同? [英] WCF Contract In Separate Assembly - How to Use it as an Endpoint Contract?

查看:586
本文介绍了WCF合同在单独的程序 - 如何使用它作为一个端点合同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查,浪费了一整天乱投医我能想到的,或找到解决这个问题:我建我的第一个WCF服务,并认为这将是冷静,以承载它在IIS中。这是一个长期运行的服务,所以这是一个不走。因为我已经增加了一个项目,我的解决方案来承载WCF服务着,所引用的WCF项目,并已先后与不断奋斗试图从code到主机。 我主要是跟着 http://msdn.microsoft.com/en-us/library/ ms733069.aspx 为指导,贯彻主机,用小的调整我的具体情况。

目前的挑战是简单地让在托管项目的App.config中定义的服务端点。它解决不了一个端点的合同属性,我的WCF服务合同。

WCF大会

 命名空间WcfFoldingService
{
    [的ServiceContract(SessionMode = SessionMode.Required,CallbackContract = typeof运算(IFoldingUpdaterClientContract))]
    公共接口IFoldingUpdaterServiceBehavior
    {
        [OperationContract的(IsOneWay =假,IsInitiating =真)
        空隙订阅();
        [OperationContract的(IsOneWay =假,IsTerminating =真)
        无效退订();
        [OperationContract的(IsOneWay =真)
        无效PublishSomeOperation();
    }
    公共接口IFoldingUpdaterClientContract
    {
        [OperationContract的(IsOneWay =真)
        无效SomeOperation();
    }
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,ConcurrencyMode = ConcurrencyMode.Single)
    公共类FoldingUpdaterService:IFoldingUpdaterServiceBehavior
    {
        //不再赘述,但完整的实现存在。
    }
}
 

/ WCF大会

主持大会

 的App.config
< system.serviceModel>
<服务>
  <服务名称=WcfFoldingService.FoldingUpdaterServicebehaviorConfiguration =htt​​pServiceBehavior>
    <端点地址=绑定=wsDualHttpBindingbindingConfiguration =ServiceBindingConfiguration合同=WcfFoldingService.IFoldingUpdaterServiceBehavior/>
    <端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/>
  < /服务>
< /服务>
< protocolMapping>
  <新增计划=HTTP绑定=wsDualHttpBindingbindingConfiguration =ServiceBindingConfiguration/>
< / protocolMapping>
<绑定>
  < wsDualHttpBinding>
    <绑定名称=ServiceBindingConfiguration>
      <有序的ReliableSession =真/>
      <安全模式=无/>
    < /装订>
  < / wsDualHttpBinding>
< /绑定>
<行为>
  < serviceBehaviors>
    <行为NAME =htt​​pServiceBehavior>
      < serviceMetadata httpGetEnabled =真/>
      < serviceDebug includeExceptionDetailInFaults =FALSE/>
      < serviceThrottling maxConcurrentCalls =1000maxConcurrentInstances =100maxConcurrentSessions =100/>
      < sendMessageChannelCache allowUnsafeCaching =FALSE/>
    < /行为>
  < / serviceBehaviors>
< /行为>
< /system.serviceModel>
 

/主持大会

 消息:
该契约属性无效 - 值'WcfFoldingService.IFoldingUpdaterServiceBehavior根据其数据类型'serviceContractType'是无效的 - 失败枚举约束。
 

我难倒。谷歌提供了一些相关的链接,并且它们大多都与,我没有遇到其他的配置问题。 WCF的组件引用,我使用完全合格的名字,我甚至尝试使用的情况下出现了一个命名空间冲突不知何故ConfigurationName属性。我是新来的WCF所以我希望这是一个明显的问题!

编辑: 我现在使用的程序化配置,而不​​是App.config的XML。最显著语句是 ContractDescription.GetContract(的serviceType),因为即使新ContractDescription(FullNamespace.IContract)失败正是因为XML配置一样。为的serviceType的真实姓名属性的类型的对象是完全一样的我FullNamespace.IContract。我怀疑一个问题,不被不使用硬盘提到它在code加载的程序集,但在这一点上,我不能肯定。目前,这种方法将正常工作:

 使用(VAR主机=新的ServiceHost(typeof运算(FoldingUpdaterService),baseAddress))
{
    VAR的serviceType = typeof运算(IFoldingUpdaterServiceBehavior);
    VAR的ServiceContract = ContractDescription.GetContract(的serviceType);
    VAR foldingEndpoint =新ServiceEndpoint(的ServiceContract)
        {
            绑定=新WSDualHttpBinding()
            地址=新的EndpointAddress(新Properties.Settings()。WcfUpdaterServiceAddress)
        };
    host.Description.Endpoints.Add(foldingEndpoint);
    [...]
 

解决方案

这是一个老问题,但万一有人有同样的问题,这为我工作: 请尝试关闭Visual Studio和删除位于相同的目录解决方案文件(的.sln)的文件名为.suo。这将重置编辑缓存。我有同样的问题后,我搬到了服务合同文件,以不同的装配和高速缓存仍然提到了原来的位置。

I've searched and wasted all day trying everything I can think of or find to resolve this issue: I built my first WCF service and thought it would be cool to host it in IIS. It's a long-running service, so that's a no-go. I have since added another project to my solution to host the WCF service with, referenced the WCF project, and have been met with constant struggle trying to host from code. I largely followed http://msdn.microsoft.com/en-us/library/ms733069.aspx for guidance on implementing the host, with minor tweaks for my specific situation.

The current challenge is simply getting the service endpoint defined in the hosting project's App.config. It cannot resolve the "contract" attribute of an "endpoint" to my WCF service contract.

WCF Assembly

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 Assembly

Hosting Assembly

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>

/Hosting Assembly

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

I'm stumped. Google offers few relevant links, and they mostly have to do with other configuration issues that I am not experiencing. The WCF assembly is referenced, I am using fully qualified names, I even tried using the ConfigurationName attribute in case there was a namespace conflict somehow. I'm new to WCF so I'm hopeful that this is an obvious problem!

Edit: I am now using programmatic configuration rather than App.config XML. The most significant statement is ContractDescription.GetContract(serviceType), as even new ContractDescription("FullNamespace.IContract") failed exactly as the XML configuration did. The Type object for serviceType's FullName property is exactly the same as my "FullNamespace.IContract". I suspect an issue with the assembly not being loaded without a hard reference to it in code, but I cannot be certain at this point. For now, this approach will work fine:

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);
    [...]

解决方案

This is an old question, but in case someone has the same problem, this worked for me: Try closing Visual Studio and deleting the .suo file located at the same directory as your solution file (.sln). This resets the editors cache. I had the same problem after I moved the service contract file to a different assembly and the cache still referred to the old location.

这篇关于WCF合同在单独的程序 - 如何使用它作为一个端点合同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆