简体   繁体   English

向DataContract添加名称和命名空间有什么作用?

[英]What does adding Name and Namespace to DataContract do?

I tried calling a WebInvoke method called Register which returns takes in a User object and immediately just returns that object. 我尝试调用一个名为Register的WebInvoke方法,该方法返回一个User对象并立即返回该对象。 It looks like the following: 它看起来如下:

User Register(User user)
{
    return user;
}

I am not sure what the Name and Namespace attributes do to the DataContract attribute when calling http://localhost:8081/user/register for example? 我不确定在调用http:// localhost:8081 / user / register时,Name和Namespace属性对DataContract属性的作用是什么?

The reason I ask is because I initially had my class decorated with the DataContract attribute like this: 我问的原因是因为我最初使用DataContract属性装饰了我的类,如下所示:

[DataContract]
public class User
{
   // Properties
}

When I opened up Fiddler, and sent a Post request, it said method not allowed, but when I changed DataContract to: 当我打开Fiddler并发送一个Post请求时,它说方法不允许,但当我将DataContract更改为:

[DataContract(Name="User", Namespace="")]

It worked. 有效。

In additional to the other answers, the Namespace in a DataContract allows for two objects of the same name in different namespaces - ie versioning. 除了其他答案之外,DataContract中的命名空间允许在不同的命名空间中使用两个相同名称的对象 - 即版本控制。

These two objects are allowed to exist as different properties in a WSDL and will be known deserializable types provided they have different namespaces: 允许这两个对象作为WSDL中的不同属性存在,并且只要它们具有不同的名称空间,它们就是已知的可反序列化类型:

[DataContract(Namespace = "http://myservice/v1/thing")]
V1.Thing

[DataContract(Namespace = "http://myservice/v2/thing")]
V2.Thing

Of course, they need to exist in your C# code as well for it to be valid. 当然,它们需要存在于您的C#代码中以使其有效。 Or, alternatively you can change the name that the objects are known by using the Name attribute for clarity. 或者,您也可以使用“名称”属性更改对象已知的名称。

[DataContract(Name = "Thing")]
V1.Thing

[DataContract(Name= = "newThing")]
V2.Thing

You might use this when the class' name has changed in your project, but you need to support existing clients that use the 'old' names. 您可以在项目中更改类名称时使用此选项,但需要支持使用“旧”名称的现有客户端。

In summary, the Name and Namespace properties control how your objects will be serialized and deserialized when transmitted over the wire. 总之,Name和Namespace属性控制在通过线路传输时对象的序列化和反序列化方式。 When you set them, you are controlling how the client will see your data contract. 设置它们时,您将控制客户端查看数据合同的方式。

Johann's answer, IMO is the correct one. 约翰的回答,IMO是正确的。

It works this way because when you send SOAP messages, the elements need to be namespace qualified, otherwise the WCF doesnt know how to deserialize the SOAP into the User data contract because of the namespace mismatch. 它以这种方式工作,因为当您发送SOAP消息时,元素需要进行命名空间限定,否则WCF不知道如何将SOAP反序列化为用户数据协定,因为命名空间不匹配。

In C#, these two objects are different because they are in different namespaces... 在C#中,这两个对象是不同的,因为它们位于不同的命名空间中......

namespace UserServices
{
    public class User
    {
        public string FirstName { get; set; }
    }
}

namespace TempuriServices
{
    public class User
    {
        public string FirstName { get; set; }
    }
}

The Namespace in XML / SOAP serves the same purpose, to make sure the objects are from the same "body" / "company" / "organization" / "domain" etc. XML / SOAP中的命名空间用于相同的目的,以确保对象来自相同的“主体”/“公司”/“组织”/“域”等。

From what I have found, when I build SOAP services, I tend to keep all of my data contracts, service contracts, and binding namespaces in the same namespace, eg " http://mycompany.com/services/serviceName " 根据我的发现,当我构建SOAP服务时,我倾向于将所有数据契约,服务契约和绑定名称空间保留在同一名称空间中,例如“ http://mycompany.com/services/serviceName

here are some great resources... Data Contract Equivalence => http://msdn.microsoft.com/en-us/library/ms734767.aspx Data Contract Versioning Best Practices => http://msdn.microsoft.com/en-us/library/ms733832.aspx 这里有一些很棒的资源...... Data Contract Equivalence => http://msdn.microsoft.com/en-us/library/ms734767.aspx Data Contract Versioning Best Practices => http://msdn.microsoft.com/en -us /库/ ms733832.aspx

Hope this helps. 希望这可以帮助。

These properties control the namespace and name of the element in the WSDL. 这些属性控制WSDL中元素的名称空间和名称。 The important part in your code is the Namespace="" : this will override the default namespace (http://tempuri.org) and set its value to an empty URL. 代码中的重要部分是Namespace="" :这将覆盖默认命名空间(http://tempuri.org)并将其值设置为空URL。

In the end, the User class will be renamed in the WSDL from http://tempuri.org/User to simply User. 最后,User类将在WSDL中从http://tempuri.org/User重命名为User。

In addition to other answers, I will try to add what I know to this topic. 除了其他答案,我将尝试添加我对此主题的了解。 In short, they both overwrite the default Name and Namespace of the [DataContract] and [DataMember] (Name) with whatever you supply to these properties. 简而言之,它们都会覆盖[DataContract]和[DataMember](Name)的默认名称和命名空间,无论您提供给这些属性。 According to the MS's documentation for DataContractAttribute.Namespace property (they are called properties of the attribute, not attribute), on the 'Tip' section it states link , "For the data to be successfully transmitted, the name of the data in a data contract must be the same in both the client and the server. Visual Basic projects, by default, add a prefix to the namespace defined in each file (called the “root namespace,” named after the project). Adding this prefix causes the client and server namespaces to be different for the same type. The solution is to set the Namespace property to “” , or to explicitly set the data contract namespace in this property." 根据MS的DataContractAttribute.Namespace属性文档(它们被称为属性的属性,而不是属性),在'Tip'部分,它表示链接 ,“为了成功传输数据,数据中的数据名称客户端和服务器中的契约必须相同。默认情况下,Visual Basic项目为每个文件中定义的命名空间添加前缀(称为“根命名空间”,以项目命名)。添加此前缀会导致客户端和服务器名称空间对于相同类型是不同的。 解决方案是将Namespace属性设置为“” ,或者在此属性中显式设置数据协定名称空间。“ From what I have understood, for DataContract attribute to be able to serialize/deserialize the data, the data must have a matching namespace in both client and the server side, which might always not be the case in a real world situation. 根据我的理解,对于DataContract属性能够序列化/反序列化数据,数据必须在客户端和服务器端都具有匹配的命名空间,在实际情况中可能永远不会出现这种情况。 For instance, your data on the Server side, if named in a readable and sensible way, might be under the namespace that has a name something like "NameOfTheSolution.Server.NameOfTheProject," whereas on the client side, it could be something like "NameOfTheSolution.Client.NameOfTheProject." 例如,如果以可读和合理的方式命名,则服务器端的数据可能位于名称类似于“NameOfTheSolution.Server.NameOfTheProject”的名称空间下,而在客户端,它可能类似于“ NameOfTheSolution.Client.NameOfTheProject“。 Due to different namespace the DataContracts are in, [DataContract] attribute won't be able serialize/deserialize the data between the client and server. 由于DataContracts所在的命名空间不同,[DataContract]属性将无法序列化/反序列化客户端和服务器之间的数据。 I am not positive, but this could be the reason why it said method not allowed in your case, due to a mismatched namespace. 我不是肯定的,但这可能是它因为命名空间不匹配而在您的情况下说不允许的方法的原因。 In a situation where namespaces do not match, 'Namespace' property could be used while using the [DataContract] attribute and provide the class on the either side (client/server) with the same namespace, although they physically lay in different namespaces. 在命名空间不匹配的情况下,可以在使用[DataContract]属性时使用“Namespace”属性,并在任一侧(客户端/服务器)提供具有相同命名空间的类,尽管它们实际上位于不同的命名空间中。

[DataContract (Namespace = “Whatever you want, usually uri”)]
public class User
{}

As far as the 'Name' property of the [DataContract] attribute goes, it overrides the name of your datacontract with the name you provide to this property. 就[DataContract]属性的'Name'属性而言,它会使用您为此属性提供的名称覆盖datacontract的名称。 One use of it, in the context of DataMember attribute is to overload a method within a data contract. 在DataMember属性的上下文中,它的一个用途是重载数据协定中的方法。 A DataContract doesn't allow two DataMember with the same name, so in such scenario, 'Name' property is useful. DataContract不允许两个具有相同名称的DataMember,因此在这种情况下,“Name”属性很有用。

Based on another question and on: 基于另一个问题并且:

I am not sure what the Name and Namespace attributes do to the DataContract attribute when calling http://localhost:8081/user/register for example? 我不确定在调用http:// localhost:8081 / user / register时,Name和Namespace属性对DataContract属性的作用是什么?

I suggest that you are using REST service. 我建议你使用REST服务。 When you called the service without setting Namespace to empty string did you define User XML with namespace xmlns="http://tempuri.org"? 当您在未将Namespace设置为空字符串的情况下调用服务时,您是否使用名称空间xmlns =“http://tempuri.org”定义了User XML? If not you sent to service different/unknown "data type" and it is probably reason for returned error. 如果没有,你发送服务不同/未知“数据类型”,这可能是返回错误的原因。

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

相关问题 [DataContract]的命名空间 - Namespace for [DataContract] AspnetCore datacontract 在 JSON 中使用我的字典键的名称空间进行序列化 - AspnetCore datacontract serialize in JSON with the name namespace of the keys of my dictionary Microsoft翻译-找不到类型或名称空间名称'DataContract' - Microsoft Translate - The type or namespace name 'DataContract' could not be found 是什么导致“名称空间中不存在类型或名称空间名称'UI'”错误? - What is causing “The type or namespace name 'UI' does not exist in the namespace” error? 无法构建我的统一项目。 “命名空间中不存在类型或命名空间名称‘Speech’”; 我能做些什么来解决? (虚拟现实统一) - Cant build my unity project. "The type or namespace name 'Speech' does not exist in the namespace"; What can I do to fix? (VR Unity) 命名空间中不存在类型或命名空间名称 - 但命名空间确实存在 - Type or namespace name does not exist in the namespace - yet the namespaces do exist 更改DataContract的名称空间 - Change the namespace of DataContract 使用DataContract重构类的名称空间 - Refactoring the namespace of a class with a DataContract 如何为DataContract设置命名空间? - How to set Namespace for DataContract? 数据合同的类型或名称空间 - Type or namespace for datacontract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM