2 suyangui suyangui 于 2014.02.01 16:00 提问

WCF 中为什么OperationContext.Current为空?

照着教程写了个例子,但是调试的时候OperationContext.Current为空报错,请问是什么原因呢?代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.IdentityModel;
using System.Runtime.Serialization;
using System.Data;

namespace WCFStudy
{
    [ServiceContract]
    public interface IEchoClaims
    {
        [OperationContract]
        List<string> Echo();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.IdentityModel;
using System.IdentityModel.Claims;
using System.Runtime.Serialization;
using System.Data;

namespace WCFStudy
{
    public class EchoClaims : IEchoClaims
    {
        public List<string> Echo()
        {
            List<string> claims = new List<string>();
             '// 此处为报错结点,OperationContext.Current为空!!!!!'
             foreach (ClaimSet set in OperationContext.Current.ServiceSecurityContext.AuthorizationContext.ClaimSets)
            {
                foreach (Claim claim in set)
                {
                    claims.Add(string.Format("{0} - {1} - {2}", claim.ClaimType, claim.Resource.ToString(), claim.Right));
                }
            }

            return claims;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.IdentityModel;
using System.Runtime.Serialization;
using System.Data;

namespace WCFStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(EchoClaims), new Uri("http://localhost:8733"));
            try
            {
                host.Open();
                Console.WriteLine("Service running...");
                Console.WriteLine("Press a key to quit");
                Console.ReadKey();
            }
            finally
            {
                host.Close();
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.serviceModel>
    <services>
      <service name="WCFStudy.EchoClaims" 
               behaviorConfiguration="echoClaimsBehavior">
        <endpoint address="EchoClaims" 
                  contract="WCFStudy.IEchoClaims" 
                  binding="wsHttpBinding" 
                  bindingConfiguration="echoClaimsBinding" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="echoClaimsBinding">
          <security mode="Message">
            <message clientCredentialType="UserName" 
                     negotiateServiceCredential="true"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="echoClaimsBehavior">
          <serviceCredentials>
            <serviceCertificate findValue="CN=WCFServer" 
                                storeLocation="LocalMachine" 
                                storeName="My" 
                                x509FindType="FindBySubjectDistinguishedName"/>
            <userNameAuthentication userNamePasswordValidationMode="Windows"></userNameAuthentication>

          </serviceCredentials>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>      
    </behaviors>
  </system.serviceModel>
</configuration>
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!