我费时,需要头从.NET 2网站的WCF服务。如何以编程的头添加到邮件?费时、邮件、网站、NET

2023-09-04 03:52:43 作者:一口热风

我有一个使用自定义实例提供程序(实现IInstanceProvider)该getInstance方法预计,包含了几个头,这样的消息的WCF服务:

I have a WCF service that uses a custom instance provider (implements IInstanceProvider) for which the GetInstance method expects a message that contains a few headers, like this:

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        string token = null;

        if (message != null)
        {
            token = message.Headers.GetHeader<string>("Token", "urn:userinfo");
        }

        Bootstrapper bootstrapper = new Bootstrapper();
        bootstrapper.InitialiseSession(token);

        return new MyServiceHost(bootstrapper.ServiceModel);
    }

这个服务现在需要从一个.NET 2 Web应用程序调用,所以我增加了一个basicHttpBinding的终结点的WCF服务,并成功添加Web引用到里面加一个App_WebReferences文件夹为MyService一个文件夹内的.NET应用程序2包括WSDL文件和几个.XSD文件。

This service now needs to be called from a .NET 2 web application, so I added a basicHttpBinding endpoint to the WCF service and successfully added the web reference to that .NET 2 application which added an App_WebReferences folder with a MyService folder inside including the WSDL file and several .XSD files.

我只想澄清一点,即Web应用程序是写在VB.Net而不是C#和我不熟悉,VB.Net,因为我用C#。

Just to clarify a little, that web application is written in VB.Net instead of C# and I'm not as familiar with VB.Net as I am with C#.

所以,在Web应用程序的code我写这样的:

So, in the web application's code I wrote this:

    Private myService As MyService.MyServiceHost
    myService.Url = "http://myserver:1234/MyService"
    myService.MyMethod()

这一切工作正常,服务接收到执行的MyMethod的消息,但是,正如所料,它返回一条错误消息:没有与名令牌和命名空间骨灰盒的标题:消息中用户信息

This all works fine, the service receives the message to execute MyMethod, but, as expected, it returns an error message: "There is not a header with name Token and namespace urn:userinfo in the message."

使用我的其他客户端(C#,.NET 4.5),所有的人创造,将调用该服务的客户端时,我在code是:

With my other clients (C#, .NET 4.5), all I do in code when creating the client that will call the service is:

    MessageHeader<string> headerToken;
    OperationContext.Current = new OperationContext((IContextChannel)serviceClient);
    headerToken = new MessageHeader<string>("insert-token-here");
    OperationContext.Current.OutgoingMessageHeaders.Add(headerToken.GetUntypedHeader("Token", "urn:userinfo"));

我怎样才能做到在.NET 2应用程序类似的东西?

How can I do something similar in the .NET 2 application?

谢谢!

更新:

我现在已经设法通过创建一个不同的类继承的类MyService.MyServiceHost,然后重写方法GetWriterForMessage,这样传递给该服务的标题:

I have now managed to pass a header to the service by creating a different class that inherits the class MyService.MyServiceHost and then overriding the method GetWriterForMessage, like this:

    Protected Overrides Function GetWriterForMessage(ByVal message As System.Web.Services.Protocols.SoapClientMessage, ByVal bufferSize As Integer) As System.Xml.XmlWriter
        Dim myWriterForMessage As System.Xml.XmlWriter

        Dim myHeader As MySoapHeader
        myHeader = New MySoapHeader()
        myHeader.Token = "myToken"

        message.Headers.Add(myHeader)
        myWriterForMessage = MyBase.GetWriterForMessage(message, bufferSize)

        Return myWriterForMessage
    End Function
End Class

和我创建了一个新的类MySoapHeader继承自System.Web.Services.Protocols.SoapHeader并拥有令牌属性。

And I created a new class called MySoapHeader that inherits from System.Web.Services.Protocols.SoapHeader and has a Token property.

纵观WCF服务端,该消息是未来的头球攻门,可惜的头名是类(MySoapHeader)的名称和标题的命名空间的 http://tempuri.org ,这是不正确。

Looking at the WCF service side, the message is coming with the header, but unfortunately the header name is the name of the class (MySoapHeader) and the namespace of the header is "http://tempuri.org", which is incorrect.

我怎样才能改变这种状况,而不必命名我的课令牌(这是非常不可取的一个类名,但它是我的服务期待的标头的名称)和头的命名空间为urn:用户信息?

How can I change that without having to name my class "Token" (which is very undesirable for a class name but it is the name of the header my service is expecting) and the header's namespace to "urn:userinfo"?

再次感谢!

推荐答案

好吧,我设法这一个梳理。

Ok, I managed to sort this one out.

我创建了自己的服务类,因为我在更新中提到,并创建了自己的GetWriterForMessage方法,像这样的:

I created my own service class as I mentioned in the update, and created my own GetWriterForMessage method, like this:

Partial Public Class MyExtendedServiceHost
    Inherits MyService.MyServiceHost

    Private _token As String

    Public Sub New(ByVal token As String)
        _token = token
    End Sub

    Protected Overrides Function GetWriterForMessage(ByVal message As System.Web.Services.Protocols.SoapClientMessage, ByVal bufferSize As Integer) As System.Xml.XmlWriter
        Dim myWriterForMessage As System.Xml.XmlWriter

        Dim myTokenHeader As TokenSoapHeader
        myTokenHeader = New TokenSoapHeader()
        myTokenHeader.Value = _token

        message.Headers.Add(myTokenHeader)
        myWriterForMessage = MyBase.GetWriterForMessage(message, bufferSize)

        Return myWriterForMessage
    End Function
End Class

我还必须创建一个类为我的令牌头:

I also had to create a class for my token header:

<XmlRoot(Namespace:="urn:userinfo", ElementName:="Token", DataType:="string", IsNullable:=False)> _
Public Class TokenSoapHeader
    Inherits System.Web.Services.Protocols.SoapHeader

    <XmlText()> _
    Public Value As String
End Class

现在,我可以运行此code,没有任何问题,头被向下传递正确的:

Now I can run this code without any issues and the header is being passed down correctly:

Private myService As New MyExtendedServiceHost("my-token-here")
myService.Url = "http://myserver:1234/MyService"
myService.MyMethod()