为什么OperationContext.Current为null?

时间:2011-10-26 12:22:39

标签: .net wcf

在客户端,我需要调用此方法:

<OperationContract()> <WebMethod()> _
Public Function NotifierSignIn(ByRef url As String, ByVal login As String, ByVal password As String, ByVal sessionGuid As String, ByVal customerGuid As String) As String
    Dim ret = String.Empty
    Return ret
End Function

我试着将其称为:

//sessionGuid is empty
string url = string.Empty;
string result = ws.NotifierSignIn(ref url, txtLogin.Text, txtPassword.Password, sessionGuid, txtCustomerGuid.Text);

首先执行的是wcf service(.net 3.5)的方法:

Public Sub New()
    System.Net.ServicePointManager.ServerCertificateValidationCallback = _
        New System.Net.Security.RemoteCertificateValidationCallback(AddressOf CertificateValidationCallBack)
    _errMsgs = ErrorMessages.GetInstance()

    _authHandlerId = SoapHeaderHelper(Of Integer).GetInputHeader("HandlerId")

End Sub

我在

得到错误
public object GetInputHeader(string name)
{
    return GetHeader(name, OperationContext.Current.IncomingMessageHeaders);
}

因为OperationContext.Current为空。

我的服务是wcf服务,因为我是用户

<%@ServiceHost Language="VB" Service="MyWS.Service1" %>

有趣的是,我在小提琴手里什么都没有。

我该如何解决?

修改 遵循Marc的建议(感谢Mark)我已经将字段的初始化移到了propety。 但仍有问题。它进入我的webmethod,但在其中有下一个异常,因为OperationContext.Current仍为null。 要明确: 在我的网络方法的第一行,我有: Dim context = OperationContext.Current 不幸的是它是null:/

所以,必须有另一个因素。

这是我在客户端的app.config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MyITNotifier.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
    </sectionGroup>
  </configSections>

  <connectionStrings />

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="Service1Soap" closeTimeout="00:01:00" openTimeout="00:01:00"
          receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
          bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <client>
      <endpoint address="http://localhost:50002/MyIT.asmx" binding="basicHttpBinding"
        bindingConfiguration="Service1Soap" contract="ServiceReference1.Service1Soap"
        name="Service1Soap1" />
    </client>

  </system.serviceModel>

  <userSettings>
    <MyITNotifier.Properties.Settings>
      <setting name="UseAnimation" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="SoundFilePath" serializeAs="String">
        <value/>
      </setting>
      <setting name="UseCustomSound" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="PlaySound" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="HandlerId" serializeAs="String">
        <value>1</value>
      </setting>
      <setting name="SessionGuid" serializeAs="String">
        <value/>
      </setting>
      <setting name="MyItUrl" serializeAs="String">
        <value/>
      </setting>
      <setting name="Interval" serializeAs="String">
        <value>2</value>
      </setting>
      <setting name="RememberUser" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="CustomerGuid" serializeAs="String">
        <value/>
      </setting>
    </MyITNotifier.Properties.Settings>
  </userSettings>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>

1 个答案:

答案 0 :(得分:1)