2

これは本当に簡単な質問のように思えるかもしれませんが、私はそれをまったく理解できないようです。

新しい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.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>
4

2 に答える 2

0

MSDNには広範なサンプルがあります(方法:コードでサービスバインディングを指定する)-しかし、基本的には、次のものが必要です。

  • あなたのサービス契約(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これで、選択したベースアドレスで、コードで定義されたサービスホストが稼働しているはずです。

于 2012-11-30T16:55:36.273 に答える
0

abc (アドレス、バインディング、コントラクト) 構成を 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 ベース、トークンなどを使用できます。パラメーターのようにユーザー名とパスワードを渡すことは、最善の方法ではありません。

于 2012-11-30T16:09:02.063 に答える