2011-02-06 10 views
1

シリアル化されたオブジェクトをWCFサービスにPOSTしようとしています。しかし、私は "NotFound"エラーを受け取り続けます。私は3日間この頭を殴ってきた。誰かが私が間違っていることを教えてもらえますか?以下は、クライアントサイドのコード、WCFサービスの操作、および直列化しようとしているクラス定義です。WCFサービスへのPOST

クライアント側のコード

// 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"; 
    } 
} 

WCFプロジェクトでMyException

[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(悪い要求)ですか? –

答えて

2

.NETでWebサービスを呼び出すには、通常、クライアントプロキシを生成します。 Visual Studioでは、svcutil.exeユーティリティまたはAdd Service Reference...ダイアログを使用できます。あなたは強く型付けされたクライアントプロキシを持っていたら、あなたは単に、...

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

WCFインフラストラクチャは残りの世話をする任意のWebClients、MemoryStreams、DataContractJsonSerializersを使用せずにサービスを呼び出します。

+0

こんにちはダーリン、私はそのアプローチを理解しています。しかし、私はこのメソッドを後でAndroidアプリケーションから呼び出す必要があります。このため、クライアントサイドでオブジェクトを正しくシリアル化する方法を理解しようとしています。 – user208662

+0

@ user208662、少なくとも.NETクライアントを持っているときは、人生を単純化してみませんか? Androidクライアントを作成する必要がある場合は、.NETと同じクラスを使用しないため、ここに記述したコードは再利用できません。このアプローチは全く異なるでしょう。だから私はあなたに何を提案するのですか?正確なデータを表示したい場合は、.NETで正しく実行し、Wiresharkなどのネットワークアナライザツールを使用して、ワイヤで交換された内容を正確に確認します。 –

+0

+1(@ user208662)が.NETの外でこれを呼び出す必要がない限り。サービスはRESTfulであるように見えますが、強く型付けされたプロキシを使用せずに呼び出すことはできますが、必要がなければプロキシクラスを作成するだけです。 webClientだけを使用して呼び出す必要がある場合は、MyExceptionクラスにDataContract属性をマークアップして、Jsonがワイヤでエンドポイントが予期しているものと正確に一致するようにセリカリ化する必要があります。 (これはプロキシが別の方法であなたを助けるつもりです) –

関連する問題