2011-02-06 166 views
1

我想將一個序列化的對象POST到我的WCF服務。但是,我一直收到「NotFound」錯誤。我一直在這裏打了三天。有人能告訴我我做錯了什麼嗎?下面是我的客戶端代碼,我的WCF服務操作以及我試圖序列化的類定義。POST到WCF服務

客戶端代碼

// Build a wrapper exception that will be serialized 
Exception ex = e.ExceptionObject; 
MyException exception = new MyException(ex.Message, ex.StackTrace, "silverlight app", ex.GetType().FullName, string.Empty); 

string json = string.Empty; 
using (MemoryStream memoryStream = new MemoryStream()) 
{ 
    // Serialize the object 
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectType); 
    serializer.WriteObject(memoryStream, objectToSerialize); 

    // Convert the data to json 
    byte[] bytes = memoryStream.ToArray(); 
    int count = (int)(memoryStream.Length); 
    json = Encoding.UTF8.GetString(bytes, 0, count); 
} 

// Submit the information to log 
string url = "http://localhost:90/services/MyService.svc/LogError"; 
WebClient loggingService = new WebClient(); 
loggingService.UploadStringCompleted += new UploadStringCompletedEventHandler(loggingService_UploadStringCompleted); 
loggingService.Headers["Content-type"] = "application/json"; 
loggingService.Encoding = Encoding.UTF8; 
loggingService.UploadStringAsync(new Uri(logExceptionUrl), "POST", json); 

MyException(客戶端版)

public class MyException : Exception 
{ 
    private readonly string stackTrace; 
    public override string StackTrace 
    { 
    get { 
     return base.StackTrace; 
    } 
    } 

    private readonly string message; 
    public override string Message 
    { 
    get { 
     return base.Message; 
    } 
    } 

    private readonly string component; 
    public string Component 
    { 
    get { return component; } 
    } 

    private readonly string typeName; 
    public string TypeName 
    { 
    get { return typeName; } 
    } 

    private readonly string miscellaneous; 
    public string Miscellaneous 
    { 
    get { return miscellaneous; } 
    } 

    public MyException() 
    { } 

    public MyException(string message) : base(message) 
    { } 

    public MyException(string message, Exception inner) : base(message, inner) 
    { } 

    public MyException(string message, string stackTrace) : base() 
    { 
    this.message = message; 
    this.stackTrace = stackTrace; 
    } 

    public MyException(string message, string stackTrace, string component, string typeName, string miscellaneous) : base() 
    { 
    this.message = message; 
    this.stackTrace = stackTrace; 
    this.component = component; 
    this.typeName = typeName; 
    this.miscellaneous = miscellaneous; 
    } 
} 

WCF服務

[OperationContract] 
[WebInvoke(UriTemplate = "/LogError", BodyStyle=WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public string LogError(MyException exc) 
{ 
    try 
    { 
    // Write the details of exc to the database 
    return "ok"; 
    } 
    catch (Exception ex) 
    { 
    return "error"; 
    } 
} 

MyException在WCF項目

[Serializable] 
public class MyException : Exception 
{ 
    public override string StackTrace 
    { 
    get { return base.StackTrace; } 
    } 
    private readonly string stackTrace; 

    public override string Message 
    { 
    get { return base.Message; } 
    } 
    private readonly string message; 

    public string Component 
    { 
    get { return component; } 
    set { /* */ } 
    } 
    private readonly string component; 

    public string TypeName 
    { 
    get { return typeName; } 
    set { /* */ } 
    } 
    private readonly string typeName; 

    public string Miscellaneous 
    { 
    get { return miscellaneous; } 
    set { /* */ } 
    } 
    private readonly string miscellaneous; 

    public MyException() 
    {} 

    public MyException(string message) : base(message) 
    { } 

    public MyException(string message, Exception inner) : base(message, inner) 
    { } 

    public MyException(string message, string stackTrace) : base() 
    { 
    this.message = message; 
    this.stackTrace = stackTrace; 
    } 

    public MyException(string message, string stackTrace, string component, string typeName, string miscellaneous) : base() 
    { 
    this.message = message; 
    this.stackTrace = stackTrace; 
    this.component = component; 
    this.typeName = typeName; 
    this.miscellaneous = miscellaneous; 
    } 

    protected MyException(SerializationInfo info, StreamingContext context) : base(info, context) 
    { 
    component = info.GetString("component"); 
    typeName = info.GetString("typeName"); 
    miscellaneous = info.GetString("miscellaneous"); 
    } 

    public override void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
    base.GetObjectData(info, context); 
    info.AddValue("component", component); 
    info.AddValue("typeName", typeName); 
    info.AddValue("miscellaneous", miscellaneous); 
    } 
} 

謝謝您的幫助!

+0

當你說「找不到」時,你能否詳細說明確切的HTTP錯誤。它是404還是400(壞請求)? – 2011-02-06 14:16:07

回答

2

爲了在.NET中調用Web服務,您通常會生成一個客戶端代理。您可以在Visual Studio中使用svcutil.exe實用程序或Add Service Reference...對話框。一旦你的強類型的客戶端代理,你只需調用服務,而無需使用任何Web客戶,MemoryStreams,DataContractJsonSerializers,...

using (var client = new MyWebServiceClient()) 
{ 
    var result = client.SomeMethod(); 
} 

WCF基礎結構負責剩下的照顧。

+0

嗨達林,我明白這個方法。不過,我也想稍後從Android應用程序中調用此方法。正因爲如此,我試圖瞭解如何在客戶端正確序列化一個對象。 – user208662 2011-02-06 14:16:53