Лучший способ, который я нашел для этого, как это было указано в комментарии, с помощью 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 (или любой другой), вы просто должны добавить классы MessageInspector и Behavior внутри вашего основного класса (например, form1.cs), вам необходимо добавить часть Config в свой App.config в части 44. Компонент system.serviceModel, поставьте его перед привязками (ej: http://pastebin.com/7KS24CmW).
Я не уверен, что 'SoapExtensions' будет работать с WCF - я использовал их в прошлом с ASMX, но не с WCF. Вы можете посмотреть в расширениях WCF, таких как IDispatchMessageInspector и IClientMessageInspector. Проверьте [Расширение WCF - Инспекторы сообщений] (http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx) Карлоса Фигейры (он является частью команда WCF, или была, IIRC). – Tim