我在控制台应用程序中有一个自托管 wcf 服务。一个简单的自托管服务是没有问题的,有足够的例子。
现在我想模拟 wcf 服务的调用者。虽然我关注了这篇 msdn 文章 http://msdn.microsoft.com/en-us/library/ff648505.aspx,但我得到了以下错误:
合同操作“GetProduct”需要 Windows 身份才能自动模拟。绑定不提供代表调用者的 Windows 标识
这是我在 CONSOLE 应用程序中 wcf 服务的 App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SelfHostingWCFService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<SelfHostingWCFService.Properties.Settings>
<setting name="URL" serializeAs="String">
<value>http://localhost:8081/ProductService</value>
</setting>
</SelfHostingWCFService.Properties.Settings>
</applicationSettings>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ProductServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceAuthorization impersonateCallerForAllOperations="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="SelfHostingWCFService.ProductService"
behaviorConfiguration="ProductServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/ProductService"/>
</baseAddresses>
</host>
<endpoint address="soap" binding="basicHttpBinding" contract="SelfHostingWCFService.IProductService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
</configuration>
这是服务代码:
public class ProductService : IProductService
{
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public Product GetProduct(int productId)
{
Product prod = new Product();
prod.ID = productId;
prod.Name = productId.ToString() + " XDS";
return prod;
}
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public string User()
{
string Name = string.Empty;
if (OperationContext.Current.ServiceSecurityContext != null)
Name = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
if (ServiceSecurityContext.Current != null)
Name = ";" + ServiceSecurityContext.Current.WindowsIdentity.Name;
Name = ";" + Thread.CurrentPrincipal.Identity.Name;
return Name;
}
}
这是客户端代码:
private void button1_Click(object sender, EventArgs e)
{
ProductService.ProductService srv = new ProductService.ProductService();
//srv.Credentials = System.Net.CredentialCache.DefaultCredentials;
ProductService.Product prod = srv.GetProduct(1, true);
label1.Text = prod.Name;
label2.Text = srv.User();
}
我做错了什么?请告诉我。
我可以使用 basicHTTPBinding 还是必须使用 wsHTTPBinding?
非常感谢您的帮助