Recently, when I was working on a project, I found that the same data contract client is affiliated with two different namespaces. Therefore, if the two services interact with each other, they may involve type issues and have poor interoperability, to perform unnecessary disassembly and installation, I finally found the answer. I will describe this scenario as follows.
[Entity class] Note The namespace
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace garychenwcfservice
{
/// <Summary>
/// Test the object class
/// </Summary>
Public Class Customer
{
Public string name
{
Get;
Set;
}
Public String sex
{
Get;
Set;
}
}
}
The following is service 1. Note: The returned result is an object of the garychenwcfservice. Customer type.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace garychenwcfservice. serviceclasses
{
Public class onecustomerservice: servicecontracts. ionecustomer
{
# Region ionecustomer Member
Public garychenwcfservice. Customer getcustomer ()
{
// Omitted
}
# Endregion
The following is service 2. Note: The garychenwcfservice. Customer type object is added.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace garychenwcfservice. serviceclasses
{
Public class twocustomerservice: servicecontracts. itwocustomer
{
# Region itwocustomer Member
Public void addcustomer (garychenwcfservice. Customer CS)
{
// Omitted
}
# Endregion
}
}
The host part is omitted... let's look at the client. The problem is coming.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GaryChenClient
{
class Program
{
static void Main( string[] args )
{
OneCustomerService_Proxy.OneCustomerClient OneService_Proxy = new GaryChenClient.OneCustomerService_Proxy.OneCustomerClient();
TwoCustomerService_Proxy.TwoCustomerClient TwoService_Proxy = new GaryChenClient.TwoCustomerService_Proxy.TwoCustomerClient();
OneCustomerService_Proxy.Customer cu = OneService_Proxy.GetCustomer();
TwoService_Proxy.AddCustomer(cu);
}
}
}
The customer obtained by service 1 belongs to the namespace of onecustomerservice. Now I want to put the obtained object to service 2 for processing,
This leads to two different namespaces for the same data contract. Let's take a look at what two proxies of the Object Browser generate,
In this case, if we want to put service 2 into operation, we must display the Declaration twocustomerservice_proxy.customer for conversion.
You may often encounter this problem. Let's talk about the solution below,
According to the statement in the second edition of WCF programming, I will send the original statement to everyone.
When adding a service reference in Visual Studio 2008, you must provide a unique new namespace for each service reference. The imported type is defined in the new namespace. If you add references to two different services that share the same data contract, the problem occurs because you get two different types in two different namespaces, indicates the same data contract. However, by default, if any Assembly referenced by the client contains a data contract that matches the data contract type that has been exposed to the referenced service metadata, Visual Studio 2008 will not import it again. It should be emphasized again that the existing data Contract Reference must be in another reference assembly, rather than in the client project itself. This restriction will be provided in future Visual Studio versions, and the most convenient remedy and best practice at present is to break all the shared data contracts into the specified class libraries, and let all clients reference the assembly. Then, you can control and configure the reference assembly (if any) to coordinate with the related shared data contract through the Advanced Settings dialog box for service reference (see Figure 1-10. The "reuse types in referenced assemblies" check box is selected by default, but you can disable this function if needed. As the name suggests, you can only share data contracts, but not service contracts. You can use the single-choice button in to enable Visual Studio 2008 to reuse data contracts across all referenced sets, or restrict sharing of specific sets by selecting list items.
S refers to letting you reference the DLL of my server to the client and share the same data contract in the service configuration. See
As you can see, you need to specify the Assembly referenced by the service so that there is no namespace type problem. I know this is unreasonable, but if the client
There is no way to do this if you have already developed it, instead of calling it on other platforms. We hope that the next version of WCF can be improved !!
This article ends. If any, please point out, thank you.
My blog: www.garychen.net
Address: http://garychen.net /? P = 73