在WCF服务中设置绑定
这似乎是一个非常简单的问题,但我似乎一点也不明白 我正在尝试创建一个新的WCF服务,我还不知道如何保护它们。我正在使用自定义用户名/密码进行身份验证。我现在似乎遇到的问题是,我不知道如何定义服务来使用WSHttpBinding(在服务端,而不是客户端) 我错过了一些非常简单的东西吗?如有任何提示和/或建议,将不胜感激 编辑 以下是我目前的代码: IAccountService在WCF服务中设置绑定,wcf,wcf-binding,wcf-security,Wcf,Wcf Binding,Wcf Security,这似乎是一个非常简单的问题,但我似乎一点也不明白 我正在尝试创建一个新的WCF服务,我还不知道如何保护它们。我正在使用自定义用户名/密码进行身份验证。我现在似乎遇到的问题是,我不知道如何定义服务来使用WSHttpBinding(在服务端,而不是客户端) 我错过了一些非常简单的东西吗?如有任何提示和/或建议,将不胜感激 编辑 以下是我目前的代码: IAccountService [ServiceContract] public interface IAccountService { [Op
[ServiceContract]
public interface IAccountService
{
[OperationContract]
bool IsCardValid(string cardNumber);
[OperationContract]
bool IsAccountActive(string cardNumber);
[OperationContract]
int GetPointBalance(string cardNumber);
}
服务web.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<StructureMapServiceBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="StructureMapServiceBehavior" type="Marcus.Loyalty.WebServices.Setup.StructureMapServiceBehavior, Marcus.Loyalty.WebServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="Marcus.Loyalty.WebServices.Account.IAccountService">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_Config"
contract="Marcus.Loyalty.WebServices.Account.IAccountService"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_Config"/>
</wsHttpBinding>
</bindings>
</system.serviceModel>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IAccountService" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:59492/Account/AccountService.svc"
binding="wsHttpBinding" bindingConfiguration="BasicHttpBinding_IAccountService"
contract="ServiceReference1.IAccountService" name="BasicHttpBinding_IAccountService" />
</client>
</system.serviceModel>
</configuration>
测试应用程序(控制台应用程序)
类程序
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“请输入卡号”);
var number=Console.ReadLine();
var endPoint=新的端点地址(“http://localhost:59492/Account/AccountService.svc");
var binding=newwshttpbinding(SecurityMode.Message);
binding.Security.Message.ClientCredentialType=MessageCredentialType.UserName;
binding.Security.Transport.ClientCredentialType=HttpClientCredentialType.Basic;
var cf=新的ChannelFactory(绑定,端点);
cf.Credentials.UserName.UserName=“testuser”;
cf.Credentials.UserName.Password=“Password1!”;
var service=cf.CreateChannel();
var余额=服务。IsAccountActive(数字);
WriteLine(“\n平衡:{0:#,#}”,平衡);
Console.Write(“\n\n按Enter键继续”);
Console.Read();
}
}
测试应用程序app.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<StructureMapServiceBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="StructureMapServiceBehavior" type="Marcus.Loyalty.WebServices.Setup.StructureMapServiceBehavior, Marcus.Loyalty.WebServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="Marcus.Loyalty.WebServices.Account.IAccountService">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_Config"
contract="Marcus.Loyalty.WebServices.Account.IAccountService"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_Config"/>
</wsHttpBinding>
</bindings>
</system.serviceModel>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IAccountService" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:59492/Account/AccountService.svc"
binding="wsHttpBinding" bindingConfiguration="BasicHttpBinding_IAccountService"
contract="ServiceReference1.IAccountService" name="BasicHttpBinding_IAccountService" />
</client>
</system.serviceModel>
</configuration>
您需要将abc(地址、绑定、合同)配置定义到de web.config文件中(您也可以通过编程方式进行此操作。b部分,即绑定,您可以指定
wsHttpBinding
<system.serviceModel>
<services>
<service name = "MyNamespace.MyService">
<endpoint
address = "http://localhost:8000/MyService"
binding = "wsHttpBinding"
contract = "MyNamespace.IMyContract" />
</service>
</services>
</system.serviceModel>
如果您希望以适当的方式启用安全性,有很多文献和选项。您可以使用证书、基于windows的证书、令牌等。像参数一样传递用户名和密码并不是最好的方法。MSDN()上有大量示例-但基本上,您需要:
- 您的服务合同(
)IMyService
- 该服务的实现(
)MyService
- 创建
以承载服务的代码服务主机
// Specify a base address for the service
string baseAddress = "http://YourServer/MyService";
// Create the binding to be used by the service.
WsHttpBinding binding1 = new WsHttpBinding();
using(ServiceHost host = new ServiceHost(typeof(MyService)))
{
host.AddServiceEndpoint(typeof(IMyService), binding1, baseAddress);
host.Open();
Console.ReadLine();
}
现在,您应该让您的服务主机在您选择的基址上运行,并在代码中定义了
wsHttpBinding
。您有配置文件吗?WCF中的许多内容都可以在中定义config@marc_s-我不知道,我希望在代码中定义该部分,但如果您能提供一个示例,我会做任何可行的事情:)我认为您缺少一个基本地址。我唯一不确定的是您所说的ServiceHost
。这是指托管服务的应用程序吗?另外,我现在遇到的错误是:Content-Type-application/soap+xml;charset=utf-8不受服务支持
谢谢@mmillican:是的,ServiceHost
是应用程序中需要的类,它将“承载”服务。如果您使用IIS托管WCF服务-在这种情况下,您必须创建ServiceHostFactory
的后代,以便能够向托管您的服务的IIS提供自己的自定义版本的ServiceHost
。听起来很复杂-其实并不复杂-看一篇关于如何做到这一点的文章