Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 邮寄至WCF服务_C#_Wcf - Fatal编程技术网

C# 邮寄至WCF服务

C# 邮寄至WCF服务,c#,wcf,C#,Wcf,我正在尝试将序列化对象发布到我的WCF服务。但是,我一直收到一个“NotFound”错误。这件事我已经烦了三天了。有人能告诉我我做错了什么吗?下面是我的客户端代码、我的WCF服务操作以及我试图序列化的类定义 客户端代码 // Build a wrapper exception that will be serialized Exception ex = e.ExceptionObject; MyException exception = new MyException(ex.Message, e

我正在尝试将序列化对象发布到我的WCF服务。但是,我一直收到一个“NotFound”错误。这件事我已经烦了三天了。有人能告诉我我做错了什么吗?下面是我的客户端代码、我的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);
[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服务

// 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);
[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);
  }
}

谢谢你的帮助

为了在.NET中调用web服务,您通常会生成一个客户端代理。您可以在VisualStudio中使用该工具或对话框。拥有强类型客户机代理后,只需调用服务,而无需使用任何WebClient、MemoryStream、DataContractJSonSerializer等

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

WCF基础设施负责其余部分。

当您说“未找到”时,能否详细说明确切的HTTP错误。是404,还是400(错误请求)?嗨,达林,我理解这种方法。不过,我想稍后也从Android应用程序调用此方法。正因为如此,我现在正试图理解如何在客户端正确序列化对象。@user208662,为什么不简化您的生活,至少当您有.NET客户端时是这样?当您必须编写Android客户端时,您将无法使用与.NET中相同的类来调用它,因此您在此处编写的代码将无法重用。方法将完全不同。这是我给你的建议。在.NET中正确地执行此操作,并使用网络分析工具(如Wireshark)准确地查看通过网络交换的内容,如果您希望查看确切的数据,+1,除非(@user208662)需要在.NET之外调用此工具。该服务看起来是RESTful的,在这种情况下,您可以调用w/o强类型代理,但除非您有需要,否则只需构建一个代理类。如果您需要仅使用webClient调用,则需要使用DataContract属性标记MyException类,并真正整理您的序列化,以确保通过连接的Json完全符合端点的期望。(这是代理将要帮助你的)100%同意Darin的最后评论@user208662,要正确序列化,您需要对数据进行处理,使其与服务层期望的数据契约完全匹配。最简单的方法是获取一些帮助(.net proxy),使其正常工作,然后使用序列化类将您从现在的位置(上面的示例)带到想要的位置(即.net proxy提供给您的超链接版本),也许我当时的假设不恰当。我想写一个iphone、wp7或android应用程序可以发布到的服务操作。你是说我需要为每个客户端编写一个单独的操作吗?或者,是否有一种方法来构造我的操作,以便可以从每个客户端调用它?