具有通用服务类和合同的WCF服务?

时间:2015-01-01 22:36:40

标签: c# wcf generics web-config app-config

考虑以下非常简单的代码片段,该代码片段实现了一个Web服务,该服务只是回送客户端发送给它的任何(字符串)对象:

namespace ServiceExample
{
    //------ Generic service and interface -----------
    [ServiceContract]
    public interface IGenericService<T>
    {
        [OperationContract]
        T Echo(T subject);
    }

    public class GenericService<T>: IGenericService<T>
    {
        public T Echo(T subject)
        {
            return subject;
        }
    }

    //-- Non-generic variant where T is set to be a string --

    [ServiceContract]
    public interface INonGenericService : IGenericService<string>
    {}

    public class NonGenericService: GenericService<string>, INonGenericService
    {}
}

在app.config中我可以这样配置服务:

  <system.serviceModel>
    <services>
      <service name="ServiceExample.NonGenericService">

        <endpoint 
          address="http://localhost:1111/" 
          binding="basicHttpBinding" 
          contract="ServiceExample.INonGenericService" />

      </service>
    </services>
 </system.serviceModel>

这很有效。

但是,我非常恼火,因为必须定义服务类(和服务契约)的那些空的非泛型版本,其唯一目的是将一些有效的非泛型名称放入.config文件中。

为什么我不能直接在.config文件中指定 ,我想要泛型类和接口的字符串版本?我想它可以这样做:

  <system.serviceModel>
    <services>
      <service name="ServiceExample.GenericService`1[[System.String]], mscorlib">

        <endpoint
          address="http://localhost:1111/"
          binding="basicHttpBinding"
          contract="ServiceExample.IGenericService`1[[System.String]], mscorlib" />

      </service>
    </services>
  </system.serviceModel>

我努力了,但到目前为止没有成功。这根本不可能吗?

0 个答案:

没有答案