[英]Setting binding in WCF service
This may seem like a really easy question but I can't seem to figure it out at all.这似乎是一个非常简单的问题,但我似乎根本无法弄清楚。
I'm trying to create a new WCF service, and I'm new to having to secure them.我正在尝试创建一个新的 WCF 服务,而且我对必须保护它们还很陌生。 I'm using a custom username/password for authentication.
我正在使用自定义用户名/密码进行身份验证。 The problem [right now anyways] that I seem to be running into is that I can't figure out how to define the service to use the WSHttpBinding (on the service side, not the client side).
我似乎遇到的问题 [无论如何现在] 是我无法弄清楚如何定义服务以使用 WSHttpBinding(在服务端,而不是客户端)。
Am I missing something incredibly simple?我错过了一些非常简单的事情吗? Any pointers and/or recommendations would be greatly appreciated!
任何指示和/或建议将不胜感激!
EDIT编辑
Here's my code so far: IAccountService到目前为止,这是我的代码: IAccountService
[ServiceContract]
public interface IAccountService
{
[OperationContract]
bool IsCardValid(string cardNumber);
[OperationContract]
bool IsAccountActive(string cardNumber);
[OperationContract]
int GetPointBalance(string cardNumber);
}
Service web.config服务 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>
Testing app (console app)测试应用程序(控制台应用程序)
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter card number");
var number = Console.ReadLine();
var endPoint = new EndpointAddress("http://localhost:59492/Account/AccountService.svc");
var binding = new WSHttpBinding(SecurityMode.Message);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
var cf = new ChannelFactory<IAccountService>(binding, endPoint);
cf.Credentials.UserName.UserName = "testuser";
cf.Credentials.UserName.Password = "Password1!";
var service = cf.CreateChannel();
var balance = service.IsAccountActive(number);
Console.WriteLine("\nBALANCE: {0:#,#}", balance);
Console.Write("\n\nPress Enter to continue");
Console.Read();
}
}
Testing app app.config测试应用程序 app.config
<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>
You need to define the abc (address, binding, contract) configuration into de web.config file (you can also do it programmatically. the b part, the binding, you can specify the wsHttpBinding
您需要将 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>
If you wish to enable security in a proper way, there is a lot of literature and options.如果您希望以适当的方式启用安全性,有很多文献和选项。 You can use certificates, windows based, tokens, ... passing a username & password like a parameter could not be the best way to do it.
您可以使用证书、基于 Windows 的令牌等,像参数一样传递用户名和密码并不是最好的方法。
There is an extensive sample on MSDN ( How to: Specify a Service Binding in code ) - but basically, you need to have: MSDN 上有一个广泛的示例(如何:在代码中指定服务绑定)——但基本上,您需要:
IMyService
)IMyService
)MyService
)MyService
)ServiceHost
to host your serviceServiceHost
以托管服务的代码You got all of that?你得到了所有这些吗? Great!
伟大的!
In that case, just do something like this:在这种情况下,只需执行以下操作:
// 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();
}
and now you should have your service host up and running, on your chosen base address and with the wsHttpBinding
defined in code.现在您应该在您选择的基地址上启动并运行您的服务主机,并使用代码中定义的
wsHttpBinding
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.