简体   繁体   English

WCF是由两个Web服务公开的相同类型

[英]WCF the same type exposed by two web services

I have 2 webservices that expose the same type. 我有2个公开相同类型的Web服务。

WebService1{
    public MyData Retrive(int id){...}
}
WebService2{
    public void Save(MyData x){...}
}

I use these 2 services like: 我使用这两个服务,如:

var x=client1.Retrieve(3);
client2.Save(x);

Problem: x is of type WebService1Client.MyData; 问题:x的类型为WebService1Client.MyData; Save requres x of type WebService2Client.MyData; 保存需要WebService2Client.MyData类型的x;

MyData is the same type, exposed by 2 webservices. MyData是同一类型,由2个Web服务公开。 I can't put both methods in the same servie. 我不能把两种方法放在同一个服务器上。 What should I do? 我该怎么办?

Define MyData in a separate class library that is then referenced by both your web services. 在单独的类库中定义MyData ,然后由Web服务引用。 Also reference this shared library in your clients, and when you add the service references, make sure the option to reuse types that exist is selected. 还要在客户端中引用此共享库,并在添加服务引用时,确保选中了重用现有类型的选项。 Then both sides of both WCF services will be using exactly the same type. 然后,两个WCF服务的两端将使用完全相同的类型。

you can declare your class MyData in another assembly, class library, and reference that assembly from the web services and also from the client code which calls those services. 您可以在另一个程序集,类库中声明您的类MyData,并从Web服务以及调用这些服务的客户端代码引用该程序集。

You could also consider the proper inheritance and have MyData as derived class of a base class in case this would help you, then your services only work with the base class. 您还可以考虑正确的继承,并将MyData作为基类的派生类,以防这对您有所帮助,那么您的服务只能与基类一起使用。

in fact WCF helps a lot about this because it introduces the concept of DataContract, which you should use properly in this case. 事实上,WCF对此有很大的帮助,因为它引入了DataContract的概念,在这种情况下你应该正确使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM