在WCF服务中设置绑定

时间:2012-11-30 15:55:30

标签: wcf wcf-binding wcf-security

这似乎是一个非常简单的问题,但我似乎无法弄明白。

我正在尝试创建一个新的WCF服务,我不得不保护它们。我正在使用自定义用户名/密码进行身份验证。我现在遇到的问题[现在无论如何]是我无法弄清楚如何定义服务以使用WSHttpBinding(在服务端,而不是客户端)。

我错过了一些非常简单的东西吗?任何指针和/或建议将不胜感激!

修改

到目前为止,这是我的代码: 的 IAccountService

[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>

测试应用(控制台应用)

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();
    }
}

测试app 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>

2 个答案:

答案 0 :(得分:0)

您需要将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,令牌,...传递用户名和&amp;像参数这样的密码不是最好的方法。

答案 1 :(得分:0)

MSDN(How to: Specify a Service Binding in code)上有大量示例 - 但基本上,您需要:

  • 您的服务合约(IMyService
  • 该服务的实施(MyService
  • 您创建ServiceHost以托管服务的代码

你得到了所有这些?太好了!

在这种情况下,只需执行以下操作:

// 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上启动并运行您的服务主机。