使用接口通过WCF [英] Using Interfaces With WCF

查看:142
本文介绍了使用接口通过WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索,现在读了几个小时,我无法找到任何人,我的特定情况下处理...

I have Googled and read for hours now and I can't find anyone that deals with my specific scenario...

我想用接口,我的WCF服务合同,以松散耦合从上线的每一端使用的类的服务。这将使我们有一个只包含了服务和数据合同(只是接口)低级汇编,我们可以交给顾问。在线材他们结束时,他们可以实例化实现我们的数据合同接口的数据类,它通过网络发送给我们,我们的WCF服务将再转换成/投/不管是输入数据到的我们的版本实现相同的接口数据类的。

I want to use interfaces in my WCF service contracts to loosely couple the service from the classes used on each end of the wire. This will enable us to have a low-level assembly that contains just the Service and Data Contracts (just interfaces) that we can hand to a consultant. On their end of the wire they can instantiate their data classes that implement our Data Contract interface, send it over the wire to us, and our WCF service will then translate/cast/whatever that incoming data into our version of a data class that implements the same interface.

下面是一个例子。 IDataContract 包含裸露的信息,我想通过传输线路。端点和其他WCF特定的配置都是默认的东西(我的问题可能在于这一点,所以我可以有更多的,如​​果这就是我需要改变的东西)。

Here's an example. IDataContract contains the bare information I want to transmit over the wire. The endpoints and other WCF-specific config are all default stuff (my problems may lie in that, so I can include more of it if that's where I need to change things).

修改:我已经包含更多的code,并更名为一对夫妇的课程,以帮助它不那么混乱。名称和放大器;命名空间增加的DataContractAttributes,以及在配置文件中的两个部分是基于从这个博客帖子。如果我切换了接口的抽象基类来代替,它的工作原理。不过,我希望得到这个工作有可能的话一个接口。

EDIT: I've included more of the code and renamed a couple classes to help it be less confusing. The Name & Namespace additions to the DataContractAttributes, as well as the two sections in the config files are new additions based on information from this blog post. If I switch to an abstract base class instead of an interface, it works. However, I'd like to get this working with an interface if possible.

共享库(我的code,与客户共享作家):

Shared library (my code, shared with client authors):

public interface IDataContract
{
    string MyProperty { get; set; }
}
[ServiceContract]
public interface ITestService
{
    [OperationContract]
    IDataContract TestSharedInterface(IDataContract clientData);
}

客户端code(自己的):

Client code (their's):

[DataContract(Name = "IDataContract", Namespace = "http://services.sliderhouserules.com")]
public class ClientDataClass : IDataContract
{
    [DataMember]
    public string MyProperty { get; set; }
}
private static void CallTestSharedInterface()
{
    EndpointAddress address = new EndpointAddress("http://localhost/ServiceContractsTest.WcfService/TestService.svc");
    ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>("ITestService", address);
    ITestService proxy = factory.CreateChannel();
    ((IClientChannel)proxy).Open();

    IDataContract clientData = new ClientDataClass() { MyProperty = "client data" };
    IDataContract serverData = proxy.TestSharedInterface(clientData);
    MessageBox.Show(serverData.MyProperty);
}

客户端配置:

Client config:

<system.runtime.serialization>
    <dataContractSerializer>
        <declaredTypes>
            <add type="ServiceContractsTest.Contracts.DataContracts.IDataContract, ServiceContractsTest.Contracts">
                <knownType type="ServiceContractsTest.WcfClient.ClientDataClass, ServiceContractsTest.WcfClient"/>
            </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

服务器code(矿):

Server code (mine):

public class TestService : ITestService
{
    public IDataContract TestSharedInterface(IDataContract clientData)
    {
        ServerDataClass convertedClientData = (ServerDataClass)clientData;
        IDataContract serverData = new ServerDataClass() { MyProperty = convertedClientData.MyProperty + " + server data added" };
        return serverData;
    }
}
[DataContract(Name = "IDataContract", Namespace = "http://services.sliderhouserules.com")]
public class ServerDataClass : IDataContract
{
    [DataMember]
    public string MyProperty { get; set; }
}

服务器配置:

Server config:

<system.runtime.serialization>
    <dataContractSerializer>
        <declaredTypes>
            <add type="ServiceContractsTest.Contracts.DataContracts.IDataContract, ServiceContractsTest.Contracts">
                <knownType type="ServiceContractsTest.WcfService.ServerDataClass, ServiceContractsTest.WcfService"/>
            </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

我在客户端调用抱怨已知类型得到一个序列化错误。我只是缺少客户端类中的一些元数据标记?我在一个损失到哪儿知道的问题,甚至谎言,因为我已经尝试了所有的搜索,我能想到的,没有人似乎已经处理了这个特定的场景。

I am getting a serialization error on the client call complaining about known types. Am I just missing some metadata markup in that client class? I'm at a loss as to where to even know the problem even lies, as I've tried all the searches I can think of and no one seems to have dealt with this specific scenario.

基本上,我想 ClientDataClass 来序列化到&LT; IDataContract&GT;&LT; myProperty的&GT;客户数据和LT; / myProperty的&GT;&LT; / IDataContract&GT; ,然后可以反序列化到一个 ServerDataClass 实例。这似乎应该是可能的。

Basically, I want ClientDataClass to serialize to <IDataContract><MyProperty>client data</MyProperty></IDataContract> and then be able to deserialize that into a ServerDataClass instance. This seems like it should be possible.

推荐答案

如果您的数据合同接口,WCF不知道什么对象实例化传入的请求。没有必要为类是一样的,在服务,之后所有的添加服务引用读取WSDL和产生新的类基于WSDL中的类型信息。

If your data contracts are interfaces WCF can't know what object to instantiate for an incoming request. There is no need for the class to be the same as in the service, after all the Add Service Reference reads the WSDL and generates new classes based on the type info in the WSDL.

这篇关于使用接口通过WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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