Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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服务的请求和响应保存到DB表中_C#_Wcf - Fatal编程技术网

C# 如何将WCF服务的请求和响应保存到DB表中

C# 如何将WCF服务的请求和响应保存到DB表中,c#,wcf,C#,Wcf,我有一个WCF服务,这是由一些客户消费。 现在,我想为每个请求记录此服务调用的详细信息请求和响应 我正在尝试使用web.config中的soapextensiontypes登录 <webServices> <soapExtensionTypes> <add type="Namespace.Classname,Assemblyename" priority="1" group="High"/> </soapExtensionTypes>

我有一个WCF服务,这是由一些客户消费。 现在,我想为每个请求记录此服务调用的详细信息请求和响应

我正在尝试使用web.config中的soapextensiontypes登录

<webServices>
  <soapExtensionTypes>
    <add type="Namespace.Classname,Assemblyename" priority="1" group="High"/>
  </soapExtensionTypes>
</webServices>
但是有些怎么,我无法登录

web.config中是否有我缺少的东西

<webServices>
  <soapExtensionTypes>
    <add type="Namespace.Classname,Assemblyename" priority="1" group="High"/>
  </soapExtensionTypes>
</webServices>

我们需要在使用WCF服务的客户端添加soapextension吗?

我发现最好的方法是,正如在评论中指出的那样,使用IDispatchMessageInspector和IClientMessageInspector

//First you will need to implement 2 interfaces
//IDispatchMessageInspector or IClientMessageInspector and IServiceBehavior or IEndpointBehavior

public class MustUnderstandMessageInspector : IDispatchMessageInspector
    {

        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            //Here you can use the ref request(Remember it is a ref, careful what you do with it)
            //request is the message you just received
            SaveInDatabase(request);
            return null;
        }

        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            //reply is a ref like request and it is the message you are sending
            throw new NotImplementedException();
        }
    }

// To be able to implement your MessageInspector in your service you need to create a behavior.
//this behavior will have to be added to your server.

public class UnderstandBehavior : IServiceBehavior
    {


        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
            //throw new NotImplementedException();
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            //This will Add your inspector to every dispatcher inside serviceHostBase
            //you will have to use the one your service is on
            foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
            {
                foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
                {
                    epDisp.DispatchRuntime.MessageInspectors.Add(new MustUnderstandMessageInspector());
                }
            }
        }

        void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            //throw new NotImplementedException();
        }
    }
}
这是通过编程方式完成的,要在app.config中添加行为,应该如下所示:

<system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="ServiceBehavior" type="Namespace.UnderstandBehavior"/>
      </behaviorExtensions>
    </extensions>
    <behaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehavior"></behavior>
          </serviceBehaviors>
    </behaviors>
</system.serviceModel>

要在projectoor上实现这一点,您只需在主Classform1.cs中添加MessageInspector和Behavior类,例如,然后您必须将配置部分添加到system.serviceModel部分中的App.Config中,把它放在bindingsej:.

之前,我不确定SoapExtensions是否能与WCF一起使用——我过去曾在ASMX中使用过它们,但在WCF中没有。您可以查看WCF扩展点,如IDispatchMessageInspector和IClientMessageInspector。Carlos Figueira他是WCF团队的一员,或者曾经是IIRC。非常感谢Borja的回答。我以类似的方式尝试过。但不知道为什么,我无法获得解决方案。你能给我提供一个项目文件示例吗。这将非常有帮助。非常感谢我提前更新了我的答案,但是我现在不能给你一个项目文件。我从BehaviorExtensionElement派生了UndermandBehavior类,并在web.config中更改为以使其工作