如何使用C#将Object complex(DTO)传递给WCF Post方法

时间:2014-01-10 12:00:02

标签: c# asp.net wcf

我正在努力将具有多个参数的复杂对象从客户端传递到服务器端。

Service.cs

public UserResponseData GetTestobject(UserRequestDataTest reqData,string strTest)
{
    dynamic response;

    bool IsValidUser = false;

    if (IsValidUser)
    {
        response = new UserResponseData { LoginResult = "Success" };
    }
    else
    {
        response = new UserResponseData { LoginResult = "Invalid username and password" };
    }

    return response;
}

IService.cs

[OperationContract]
[WebInvoke(
    Method = "POST", UriTemplate = "GetTestobject/", BodyStyle = WebMessageBodyStyle.Wrapped,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
UserResponseData GetTestobject(UserRequestDataTest reqData,string strTest);

// DataContract

[DataContract]
public class UserRequestDataTest
{
    [DataMember]
    public string Username { get; set; }

    [DataMember]
    public string Password { get; set; }

    [DataMember]
    public List<test> objTestList { get; set; }
}


[DataContract]
public class test
{
    [DataMember(Order = 0)]
    public string Name { get; set; }
    [DataMember(Order = 1)]
    public string Address { get; set; }
}

从客户端访问服务方法

        Uri address = new Uri("http://localhost:122/Service.svc/web/GetTestobject/");

        // Create the web request  
        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

        // Set type to POST  
        request.Method = "POST";
        request.ContentType = "application/json";


        UserRequestDataTest objUserRequestDataTest = new UserRequestDataTest();
        List<test> objtestList = new List<test>();

        test objtest = new test();
        objtest.Address = "1";
        objtest.Name = "1";
        objtestList.Add(objtest); 

        objtest = new test();
        objtest.Address = "1";
        objtest.Name = "1";
        objtestList.Add(objtest);

        objUserRequestDataTest.objTestList = objtestList;
        objUserRequestDataTest.Password = "Test";
        objUserRequestDataTest.Username= "Test";

        var json1 = new JavaScriptSerializer().Serialize(objUserRequestDataTest);

        JObject jsonNew = new JObject();
        jsonNew.Add("reqData", json1);
        jsonNew.Add("strTest", "Test");

        string data = jsonNew.ToString();

        // Create a byte array of the data we want to send  
        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);

        // Set the content length in the request headers  
        request.ContentLength = byteData.Length;

        // Write data  
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
        }

        // Get response  
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            // Get the response stream  
            StreamReader reader = new StreamReader(response.GetResponseStream());

            var statusCode = response.StatusCode;
        }

我收到此错误“远程服务器返回错误:(400)错误请求。”请指导我解决问题

1 个答案:

答案 0 :(得分:1)

我建议以这种方式使用服务: Consuming a WCF Service

然后你可以用简单的方法:

var sc = new ServiceReference1.SomeClient();
var st = sc.GetTestobject(objectParameter,"parameter1");