属性不包含在生成代理类 [英] Attribute not included in the generated proxy class

查看:123
本文介绍了属性不包含在生成代理类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET 3.0和VS2005。



在讨论的对象从WCF服务,然后序列转化成XML格式的遗留API消耗。因此,而不是序列化的TestObject,它被序列化.TestObject这是缺少[XmlRoot]属性;然而,所有的[XML *]属性的孩子,使他们的工作只是罚款因素是在生成的代理代码。因此,所有的子元素效果很好,但封闭的元素并没有因为[XmlRoot]属性不生成的代理代码包括在内。这包括[XmlRoot]属性的原始对象序列化手工精细。



我可以代理的代码包括[XmlRoot]属性,这样生成的代理类序列正确呢?如果我不能做到这一点,我怀疑我将不得不使用[XmlType将],但导致轻微的破坏,要求我改变其他组件,所以我会更喜欢前者。我也想避免手动编辑自动生成的代理类。



下面是一些示例代码(我已经包含了客户端,并在相同的应用程序服务,因为这是快速,用于测试目的。注释掉服务引用代码,并添加服务引用在运行应用程序,然后取消对服务代码并运行。)

 命名空间SerializationTest {
类节目{
静态无效的主要(字串[] args){

类型的serviceType = typeof运算(TestService的);使用
(ServiceHost的主机=新的ServiceHost(
的serviceType,
新的URI [] {
新的URI(HTTP://本地主机:8080 /)
}

))
{

的ServiceMetadataBehavior行为=新的ServiceMetadataBehavior();
behaviour.HttpGetEnabled = TRUE;
host.Description.Behaviors.Add(行为);

host.AddServiceEndpoint(的serviceType,新basicHttpBinding的(),TestService的);
host.AddServiceEndpoint(typeof运算(IMetadataExchange接口),新basicHttpBinding的(),墨西哥);


host.Open();

TestServiceClient客户端=新TestServiceClient();
localhost.TestObject为= client.GetObject();

字符串XmlizedString = NULL;
使用(MemoryStream的MemoryStream的=新的MemoryStream()){
XmlSerializer的XS =新的XmlSerializer(typeof运算(localhost.TestObject));使用(的XmlWriter的XmlWriter = XmlWriter.Create(MemoryStream的)){
xs.Serialize(XmlWriter的,到)
;
的MemoryStream =(MemoryStream的)xmlWriter.BaseStream;
XmlizedString = Encoding.UTF8.GetString(memoryStream.ToArray());
Console.WriteLine(XmlizedString);
}
}
}

Console.ReadKey();
}
}

[Serializable接口]
[XmlRoot(SomethingElse)]
公共类的TestObject {

私人布尔_worked;

公众的TestObject(){曾为= TRUE; }

[XmlAttribute(为AttributeName =AttributeWorked)]
公共BOOL曾为{
{返回_worked; }
集合{_worked =价值; }
}
}

[的ServiceContract]
公共类TestService的{

[OperationContract的]
[XmlSerializerFormat]
公众的TestObject GetObject的(){
返回新的TestObject();
}
}
}

下面是这将生成XML

 <?XML版本=1.0编码=UTF-8>?; 
<的TestObject的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =http://www.w3.org/2001/XMLSchemaAttributeWorked =真正的/>


解决方案

我发现有人谁提供了解决这种状况的一种手段



Matevz Gacnik的博客



使用 XmlAttributeOverrides ,我写以下内容:

 私有静态XmlSerializer的GetOverridedSerializer()
{
//设置的TestObject元素$覆盖b $ b XMLATTRIBUTES attrsTestObject =新XMLATTRIBUTES();
XmlRootAttribute rootTestObject =新XmlRootAttribute(SomethingElse);
attrsTestObject.XmlRoot = rootTestObject;

//创建置换器
XmlAttributeOverrides XOVER =新XmlAttributeOverrides();
xOver.Add(typeof运算(localhost.TestObject),attrsTestObject);

XmlSerializer的XSER =新的XmlSerializer(typeof运算(localhost.TestObject),XOVER);
返回XSER;
}



只要把该方法在程序类的例子,并替换的Main()

  // XmlSerializer的XS =新的XmlSerializer(typeof运算(localhost.TestObject)); 
XmlSerializer的XS = GetOverridedSerializer();



然后运行看结果。



下面是我得到:

 < XML版本=1.0编码=UTF-8>? ;< SomethingElse的xmlns:XSI =HTTP://www.w3.o 
RG / 2001 / XML模式实例的xmlns:XSD =http://www.w3.org/2001/XMLSchemaAttribu
teWorked =真/>


Using .Net 3.0 and VS2005.

The objects in question are consumed from a WCF service then serialized back into XML for a legacy API. So rather than serializing the TestObject, it was serializing .TestObject which was missing the [XmlRoot] attribute; however, all the [Xml*] attributes for the child elements were in the generated proxy code so they worked just fine. So all the child elements worked just fine, but the enclosing element did not because the [XmlRoot] attribute wasn't included in the generated proxy code. The original object that included the [XmlRoot] attribute serializes fine manually.

Can I have the proxy code include the [XmlRoot] attribute so the generated proxy class serializes correctly as well? If I can't do that I suspect I'll have to use [XmlType] but that causes minor havoc requiring me to change other components so I would prefer the former. I also want to avoid having to manually edit the autogenerated proxy class.

Here is some sample code (I have included the client and the service in the same app because this is quick and for test purposes. Comment out the service referencing code and add the service reference while running the app, then uncomment the service code and run.)

namespace SerializationTest {  
  class Program {  
    static void Main( string[] args ) {  

        Type serviceType = typeof( TestService );  
        using (ServiceHost host = new ServiceHost(   
            serviceType,   
            new Uri[] {   
                new Uri( "http://localhost:8080/" )  
            }  

        ))
        {

            ServiceMetadataBehavior behaviour = new ServiceMetadataBehavior();  
            behaviour.HttpGetEnabled = true;  
            host.Description.Behaviors.Add( behaviour );  

            host.AddServiceEndpoint( serviceType, new BasicHttpBinding(), "TestService" );  
            host.AddServiceEndpoint( typeof( IMetadataExchange ), new BasicHttpBinding(), "MEX" );  


            host.Open();  

            TestServiceClient client = new TestServiceClient();  
            localhost.TestObject to = client.GetObject();  

            String XmlizedString = null;  
            using (MemoryStream memoryStream = new MemoryStream()) {
                XmlSerializer xs = new XmlSerializer( typeof( localhost.TestObject ) );  
                using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream)) {
                    xs.Serialize( xmlWriter, to );  
                    memoryStream = (MemoryStream)xmlWriter.BaseStream;  
                    XmlizedString = Encoding.UTF8.GetString( memoryStream.ToArray() );  
                    Console.WriteLine( XmlizedString );  
                }    
            }    
        }

        Console.ReadKey();  
    }  
}  

[Serializable]  
[XmlRoot( "SomethingElse" )]  
public class TestObject {  

    private bool _worked;  

    public TestObject() { Worked = true; }  

    [XmlAttribute( AttributeName = "AttributeWorked" )]  
    public bool Worked {  
        get { return _worked; }  
        set { _worked = value; }  
    }  
}  

[ServiceContract]  
public class TestService {  

    [OperationContract]  
    [XmlSerializerFormat]  
    public TestObject GetObject() {  
        return new TestObject();  
    }  
  }  
}  

Here is the Xml this generates.

<?xml version="1.0" encoding="utf-8"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" AttributeWorked="true" /> 

解决方案

I found someone who provides a means to solve this situation:

Matevz Gacnik's Weblog

Using that approach of XmlAttributeOverrides, I wrote the following:

    private static XmlSerializer GetOverridedSerializer()
    {
        // set overrides for TestObject element
        XmlAttributes attrsTestObject = new XmlAttributes();
        XmlRootAttribute rootTestObject = new XmlRootAttribute("SomethingElse");
        attrsTestObject.XmlRoot = rootTestObject;

       // create overrider
       XmlAttributeOverrides xOver = new XmlAttributeOverrides();
       xOver.Add(typeof(localhost.TestObject), attrsTestObject);

       XmlSerializer xSer = new XmlSerializer(typeof(localhost.TestObject), xOver);
       return xSer;
    }

Just put that method in the Program class of your example, and replace the following line in Main():

        //XmlSerializer xs = new XmlSerializer(typeof(localhost.TestObject));
        XmlSerializer xs = GetOverridedSerializer();

And then run to see the results.

Here is what I got:

<?xml version="1.0" encoding="utf-8"?><SomethingElse xmlns:xsi="http://www.w3.o
rg/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Attribu
teWorked="true" />

这篇关于属性不包含在生成代理类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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