问题描述
- 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="".NETFrameworkVersion=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>
时间: 2024-10-30 14:14:48