1

構成プロパティを使用して WCF サービスを提供する「プロキシ」クラスを作成しようとしています。

これらのプロパティを app.config ファイルに保存できないため、それを「プロキシ」して、要求に応じてこれらすべてのデータを提供するカスタム configurationSection を使用する方法を探しています。そのためには、独自の ConfigurationSection を使用して system.serviceModel をロードするように .NET フレームワークに指示する必要がありますが、それができるかどうかはわかりません。

誰かが私の仮定を承認できる場合、またはさらに良い場合は、別のソースを使用して WCF 構成設定を行う方法を教えてください。

ありがとう

4

1 に答える 1

1

コードで wcf サービスを作成および登録する方法を探している場合は、以下のコードの例を確認してください。

以下は、サービス ホスト オブジェクトを作成するメソッドです。

public static ServiceHost RegisterService<T>(object service, int port, string serviceName)
{
    // Returns a list of ipaddress configuration
    IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());

    // Select the first entry. I hope it's this maschines IP
    IPAddress ipAddress = ips.AddressList[0];

    // Create the url that is needed to specify where the service should be started
    string urlService = "net.tcp://" + ipAddress.ToString() + String.Format(":{0}/{1}", port, serviceName);

    // Instruct the ServiceHost that the type that is used is a ServiceLibrary.service1
    ServiceHost host = new ServiceHost(service);
    // define events if needed
    //host.Opening += new EventHandler(HostOpeningEvent);
    //host.Opened += new EventHandler(HostOpenedEvent);
    //host.Closing += new EventHandler(HostClosingEvent);
    //host.Closed += new EventHandler(HostClosedEvent);

    // The binding is where we can choose what transport layer we want to use. HTTP, TCP ect.
    NetTcpBinding tcpBinding = new NetTcpBinding();
    tcpBinding.TransactionFlow = false;
    tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
    tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
    tcpBinding.Security.Mode = SecurityMode.None; 

    // Add a endpoint
    host.AddServiceEndpoint(typeof(T), tcpBinding, urlService);

    // A channel to describe the service. Used with the proxy scvutil.exe tool
    ServiceMetadataBehavior metadataBehavior;
    metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
    if (metadataBehavior == null)
    {
        string httpURL = "http://" + ipAddress.ToString() + String.Format(":{0}/{1}", port + 1, serviceName);

        // This is how I create the proxy object that is generated via the svcutil.exe tool
        metadataBehavior = new ServiceMetadataBehavior();
        metadataBehavior.HttpGetUrl = new Uri(httpURL);
        metadataBehavior.HttpGetEnabled = true;
        metadataBehavior.ToString();
        host.Description.Behaviors.Add(metadataBehavior);
    }
    return host;
}

使用方法は次のとおりです。

ServiceHost host = RegisterService<your_service_interface>(your_service, port, "yout_service_name");
host.Open();

これが役に立てば幸いです、よろしく

于 2009-11-18T04:17:16.407 に答える