C# 在WCF REST服务POST方法中处理Json请求数据

C# 在WCF REST服务POST方法中处理Json请求数据,c#,json,wcf,rest,wcf-rest,C#,Json,Wcf,Rest,Wcf Rest,我正在使用POST方法和对象作为输入参数创建REST服务。当客户端请求时,我无法获取客户端发布的实际JSON数据。有没有办法从C#WCF服务中挖掘JSON代码 我的代码: namespace ACTService { public class AssortmentService : IAssortmentService { public void DeleteColor(DeleteColorContarct objdelcolor) { new Met

我正在使用POST方法和对象作为输入参数创建REST服务。当客户端请求时,我无法获取客户端发布的实际JSON数据。有没有办法从C#WCF服务中挖掘JSON代码

我的代码:

namespace ACTService
{
  public class AssortmentService : IAssortmentService
  {
    public void DeleteColor(DeleteColorContarct objdelcolor)
    {
         new Methods.ColorUI().DeleteColorDetails(objdelcolor);
    }
  }
}
我的界面是

namespace ACTService
{
  [ServiceContract]
  public interface IAssortmentService
  {
    [OperationContract]
    [WebInvoke(UriTemplate = "DeleteColor", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
    void DeleteColor(DeleteColorContarct objColor);
  }
}

我需要访问其他类文件中的JSON格式ColorUI

WCF提供了许多可扩展点,其中一个是名为MessageInspector的功能。您可以创建一个自定义消息检查器来接收请求,然后再将其反序列化为C#对象。并尽可能使用原始请求数据

为了实现它,您需要实现
System.ServiceModel.Dispatcher.IDispatchMessageInspector
接口,如下所示:

public class IncomingMessageLogger : IDispatchMessageInspector
{
    const string MessageLogFolder = @"c:\temp\";
    static int messageLogFileIndex = 0;

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        string messageFileName = string.Format("{0}Log{1:000}_Incoming.txt", MessageLogFolder, Interlocked.Increment(ref messageLogFileIndex));
        Uri requestUri = request.Headers.To;

        HttpRequestMessageProperty httpReq = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];

        // Decode the message from request and do whatever you want to do.
        string jsonMessage = this.MessageToString(ref request);

        return requestUri;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
    }
}
public class InsepctMessageBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new IncomingMessageLogger());
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}
这是完整的

现在需要将此消息检查器添加到端点行为。要实现这一点,您需要实现
System.ServiceModel.Description.IEndpointBehavior
接口,如下所示:

public class IncomingMessageLogger : IDispatchMessageInspector
{
    const string MessageLogFolder = @"c:\temp\";
    static int messageLogFileIndex = 0;

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        string messageFileName = string.Format("{0}Log{1:000}_Incoming.txt", MessageLogFolder, Interlocked.Increment(ref messageLogFileIndex));
        Uri requestUri = request.Headers.To;

        HttpRequestMessageProperty httpReq = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];

        // Decode the message from request and do whatever you want to do.
        string jsonMessage = this.MessageToString(ref request);

        return requestUri;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
    }
}
public class InsepctMessageBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new IncomingMessageLogger());
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}
现在,如果您使用自托管,即以编程方式托管您的服务,那么您可以直接将这个新实现的行为附加到您的服务端点。例如

endpoint.Behaviors.Add(新IncomingMessageLogger())

但是,如果您在IIS中托管了WCF Rest服务,那么您将通过配置注入新的行为。为了实现这一点,您必须创建一个从
BehaviorExtensionElement
派生的附加类:

public class InspectMessageBehaviorExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(InsepctMessageBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new InsepctMessageBehavior();
    }
}
现在,在您的配置中,首先在
system.servicemodel
标记下注册行为:

    <extensions>
      <behaviorExtensions>
        <add name="inspectMessageBehavior" 
type="WcfRestAuthentication.MessageInspector.InspectMessageBehaviorExtension, WcfRestAuthentication"/>
      </behaviorExtensions>
    </extensions>

现在将此行为添加到端点行为:

  <endpointBehaviors>
    <behavior name="defaultWebHttpBehavior">
      <inspectMessageBehavior/>
      <webHttp defaultOutgoingResponseFormat="Json"/>
    </behavior>
 </endpointBehaviors>

在端点中设置属性
behaviorConfiguration=“defaultWebHttpBehavior”


就是这样,您的服务现在将在反序列化之前捕获所有消息。

因此对象、objColor和objdelcolor都是正确的JSON格式,您只需要在C#中解析它们即可?或者我遗漏了什么不,它们是正确的C格式。我只需要客户端发送给我的实际请求消息(JSON格式)。