简体   繁体   中英

How can I Use (Share) Datacontract or Datatype between multiple WCF service and web side

I have a service project which name is SERVICE_ABC .

(You can download my example project in this link)

In this project i defined 3 WCF services:

  • Service_A
  • Service_B
  • Service_C

This 3 Services using same data contract class in SERVICE_ABC project. For example this 3 services using PersonelDTO class as datacontract.

Web服务截图-1

Web服务截图2

My problem is this: If I call service functions which return PersonelDTO type, I have to get this result as so:

ServiceReference_A.PersonelDTO DataA = clientA.GetMyData();
ServiceReference_B.PersonelDTO DataB = clientB.GetMyData();
ServiceReference_C.PersonelDTO DataC = clientC.GetMyData();

But I want to define it as so:

PersonelDTO DataA = clientA.GetMyData();
PersonelDTO DataB = clientB.GetMyData();
PersonelDTO DataC = clientC.GetMyData();

(If I do this, visual studio give an error like "PersonelDTO is ambiguous reference between ServirceA, ServiceB and ServiceC") Namely, I want to using just one datatype in the MVC Controller. Because PersonelDTO class is same in SERVICE_ABC project. So I dont want to specialize each PersonelDTO like blabla1.PersonelDTO , blabla2.PersonelDTO

MVC项目屏幕截图

How Can I do this?

I found the solution by myself. (But I must say this , your answers was very weak.)

The solution : 1-) First at all we must generate a new Class Library. Right click on the main service solution and select
Add > Add Project > Class Library and give name SampleLibrary

2-)Generate all Data Transfer Object clases (DTO clases) in this Class Library. (For Example Right click > Add > Class and give name PersonelDTO.cs )

3-) Right click Class Library and click " Rebuild " button.

4-) Now we buid the SampleLibrary.dll file. And we will use this .dll file both of our Service Solution and MVC Web Solution. Firts at all,

Select SERVICE_ABC solution > References and right click. Select Add Reference > Browse Tab > Click Browse Button

5-) On the Browse window, select SampleLibrary.dll and add this file to WCF service as reference. (You can find this file in C:\\test\\SERVICE_ABC\\SampleLibrary\\bin\\Release\\SampleLibrary.dll or ...\\Debug\\SampleLibrary.dll path. If you build your project as Debug Mode, you will get it in Debug folder. If you build as Release Mode the .dll file will be in Release folder)

6-) Use Data Transfer Object files of Class Library in the WCF Service. For Example define in the service like this:

public FooDTO GetFoo()
    {            
        return new SampleLibrary.FooDTO() { Name = "Service A" };
    }

    public BarDTO GetBar()
    {
        //or add using SampleLibrary; definition begining of the pages 
        //and write just as BarDTO without SampleLibrary.BarDTO
        return new BarDTO() { Name = "Service A" };
    }

7-) Rebuild and run your WCF Service. (Service_ABC)

8-) Copy SampleLibrary.dll file which builded by WCF Services in the Service\\bin folders. (For example you can find it in C:\\test\\SERVICE_ABC\\SERVICE_ABC\\bin folder)

9-) Paste this SampleLibrary.dll file in your MVC Web soluiton's bin folder. (For Example you can find it in C:\\test\\WEB_ABC\\WEB_ABC\\bin folder)

10-) Select your Web Mvc solution > References and right click > Add Reference > Browse Tab > Browse button

11-) Select SampleLibrary.dll which we pasted on step-9 and so add this dll file as refrence in your Web Mvc solution also.

12-) Close your Web Solution and re-open it. (Because when you run Update Service Reference command, Solution cannot get set reused data types properly. You must restart your Vusial Studio so it can reload all dll files)

13-) Right click your Service Refeneces (In the above pictures : ServiceReference_A, ServiceReference_B or ServiceReference_C) and select "Update Service Reference"
(Be sure : Reuse types in all referenced assemblies option selected , If you dont know selected or not , Then right click your service reference and select Configure Service Reference and find it in the opened window)

All of this.. After this process, My WCF service (SERVICE_ABC) and My Web Mvc project used same Class Library dll file. And If I define multiple service in the SERVICE_ABC and I get it in the MVC side with same Type definition. Namely this codes working properly now..

//ServiceReference_A.PersonelDTO DataA = clientA.GetMyData();//old
//ServiceReference_B.PersonelDTO DataB = clientB.GetMyData();//old 
//ServiceReference_C.PersonelDTO DataC = clientC.GetMyData();//old

PersonelDTO DataA = clientA.GetMyData();//new definition
        PersonelDTO DataB = clientB.GetMyData();//new definition
        PersonelDTO DataC = clientC.GetMyData();//new definition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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