我正在开发一个小应用程序,使用wcf服务提供数据。 我想为每个消息请求添加一些标头,所以我创建了一个继承自IContractBehavior的属性,然后将其应用到我的服务合同接口:
// SecuredServiceAttribute.cs
[AttributeUsage(AttributeTargets.Interface, Inherited = true)]
public class SecuredServiceAttribute : Attribute, IContractBehavior
{
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(new ServerMessageInspector());
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
}
我的ClientMessageInspector类如下:
// ClientMessageInspector.cs
public class ClientMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageHeader header = MessageHeader.CreateHeader("smheader", "ns", "smvalue");
request.Headers.Add(header);
return null;
}
}
和我的ServerMessageInspector:
// ServerMessageInspector.cs
public class ServerMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
int found = request.Headers.FindHeader("smheader", "ns");
if (found != -1)
{
string header = request.Headers.GetHeader<string>("smheader", "ns");
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
现在我将SecuredServiceAttribute应用于合同
// IService1.cs
[ServiceContract]
[SecuredService]
public interface IService1
{
[OperationContract]
string GetData();
}
问题是:我在BeforeSendRequest和AfterReceiveRequest上放了一些断点,但只是AfterReceiveRequest被击中,所以request.Headers.FindHeader(&#34; smheader&#34;,&#34; ns&#34;)总是返回-1因为没有运行BeforeSendRequest。
这是服务器端的服务模型部分:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
和客户方:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:59814/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="Service.IService1"
name="BasicHttpBinding_IService1" />
</client>
你能帮我找一下我代码中的错误吗?
抱歉,我不擅长英语。