0

我有一个公共类(通常意味着它被定义为它自己的库,由客户端和服务使用)

    [Serializable]
    Class C1
    {
        string s1;
        string s2;
    }

在我的 WCF 服务中,我创建了一个实例,填充并返回它:这是接口:

    [ServiceContract]
    public interface myInterface
    {
        [OperationContract]
        c1 GetClass(string number);
    }

这是我的服务类:

    public class ColorTracker : myInterface
    {

        public c1 GetObj(string value)
        {
            c1 theColor = new c1();
            c1.s1= "value1";
            c1.s2= "value2";
            return  c1;
        }
    }

客户:

    using (ServieClient bmClient = new ServiceClient())
    {
        c1 theColor;

        theColor =  (c1) bmClient.GetObj("test");

    }

这一切都有效,但我只能通过支持字段访问返回数据。我想实例化对象(或者我想它可能是一个结构,因为它只有数据,而不是状态)

提前致谢。

4

2 回答 2

0

When your attribute is [Serializable] then in the client side you can just use private fields! but,since your properties are Auto genenrated fiels {get;set;} ,in the client side you can see auto generated prop__BackingFields;

you can Refactor your properties like this:

[Serializable ]
public class MyData
{
    private string _prop1;

    public  string Prop1
    {
        get { return _prop1; }
        set { _prop1 = value; }
    }

but I think the best way to code your issue is this:

1.Define two data contracts:

[DataContract  ]
public class ServerSideFilledData
{...}and
[DataContract  ]
public class ClientSideFilledData
{...}

2.Define a Message Contract with two MessageBody Attributes:

[MessageContract]
public class MixedServerandClientSideData
{
    private ClientSideFilledData clientSideFilledData;
    [MessageBodyMember]
    public ClientSideFilledData ClientSideFilledData
    {
        get { return clientSideFilledData; }
        set { clientSideFilledData = value; }
    }

    private ClientSideFilledData serverSideFilledData;
    [MessageBodyMember]
    public ClientSideFilledData ServerSideFilledData
    {
        get { return serverSideFilledData; }
        set { serverSideFilledData = value; }
    }
}

3.Instead of Serializable Or DataContract, use your Message for exchanging data between cient and server

[ServiceContract]
public interface IService1
{
    [OperationContract]
    MixedServerandClientSideData DoWork();
}

4.I think you know the continue ,initialize and fill your data in convenient sides.

于 2013-06-10T17:07:32.027 回答
0

WCF 不关心[Serializable]属性。如果你想让你的类成为一个数据契约,你应该用[DataContract]属性来标记它。

此外,将您的字段转换为属性,并用[DataMember]属性标记它们。

[DataContract]
Class C1
{
    [DataMember]
    public string s1 {get;set;}

    [DataMember]
    public string s2 {get;set;}
}
于 2013-06-09T21:14:31.057 回答