【问题标题】:Configure WCF where ServiceContract and service implementation are in separate assemblies配置 WCF,其中 ServiceContract 和服务实现位于单独的程序集中
【发布时间】:2015-09-03 04:38:00
【问题描述】:

我在这个网站上查看了很多讨论的问题,但没有直接回答这个问题。我有以下内容:

Library.dll:

namespace LibraryNamespace
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        void Operation();
    }
}

Implementation.dll:

namespace ImplementationNamespace
{
    public class ServiceImplementation : IService
    {
        public void Operation()
        {
            // Do Something
        }
    }
}

app.config:

<service name="ImplementationNamespace.ServiceImplementation">
    <endpoint 
        address="ServiceImplementation" 
        binding="netTcpBinding" 
        contract="LibraryNamespace.IService" />
    ....
</service>

我一直收到contract="LibraryNamespace.IService" 的警告。程序运行了,但我感觉这个警告给我带来了更多的问题。

“合同”属性无效 - 值 'LibraryNamespace.IService' 根据其数据类型无效 'serviceContractType' - 枚举约束失败。

ServiceContract 和服务实现在同一个程序集和命名空间中时它可以工作,但由于某种原因,它在这里不起作用。如何正确引用?

【问题讨论】:

  • 为什么要将它们放在单独的 dll 中?有什么具体原因吗?
  • 是的,基本上,我想创建一个通用的 ServiceContract,然后在不同的程序集中编写许多其他 ServiceImplementation 类,它们都遵循相同的服务合同。这样,我可以标准化我的 ServiceImplementations,并从客户端以标准方式调用它们。

标签: c# wcf namespaces app-config wcf-endpoint


【解决方案1】:

我不确定为什么要将合同和实现放在单独的 dll 中?有什么具体原因吗?通常它们将在同一个程序集中,因此在配置文件中您可以轻松引用它们。解决此问题的一种方法是在运行时创建服务端点,如下所示。

在您的托管项目中引用 dll Library.dllImplementation.dll 并使用以下代码添加端点

using LibraryNamespace;
using ImplementationNamespace;

// Specify a base address for the service
String baseAddress = "http://localhost/ServiceImplementation";

// Create the tcp binding
NetTcpBindings tcp = new NetTcpBindings();

// Define service and Create the endpoint
 using(ServiceHost host = new ServiceHost(typeof(ServiceImplementation)))
 {
  host.AddServiceEndpoint(typeof(IService),tcp, baseAddress);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多