如何将WCF服务的请求和响应保存到数据库表中

时间:2015-03-10 17:54:17

标签: c# wcf

我有一个由一些客户端承担的WCF服务。 现在我想为每个请求记录此服务调用的详细信息(请求和响应)。

我正在尝试使用web.config中的soapextensiontypes进行日志记录

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

但有些如何,我无法登录。

web.config中是否遗漏了任何内容

我们是否需要在客户端(谁使用WCF服务)添加soapextension?

1 个答案:

答案 0 :(得分:0)

我发现这样做的最好方法是,就像在评论中指出的那样,使用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();
        }
    }
}

完成两个界面后,您必须将行为添加到您的服务中。

            svcHost = new ServiceHost(typeof(Service), eventendpoint);
            svcHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding, "Endpoint");
            //Add Service Behavior
            UnderstandBehavior behavior = new UnderstandBehavior();
            svcHost.Description.Behaviors.Add(behavior);
            //////
            svcHost.Open();
            textBox1.Text = textBox1.Text + "\n\nService is Running";
            svcHost.Close();
    }

这是以编程方式完成的,要在app.config中添加行为,它将是这样的:

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

要在你的projecto(或任何)上实现这一点,你只需要在Main Class(例如form1.cs)中添加MessageInspector和Behavior类,然后你必须将Config部分添加到你的app.config里面。 system.serviceModel 部分,将它放在绑定之前(ej:http://pastebin.com/7KS24CmW)。