2

セルフホステッド サービスでは、App.config で指定されたエンドポイント (存在する場合)、または App.config が空の場合はコードで指定された既定のエンドポイントを使用したいと考えています。これどうやってするの?

編集:明確にするために、これはServiceHostを使用したサーバー(サービス)側にあります。

4

4 に答える 4

2

1 つの方法はtry、構成ファイルからのロードを最初に試行し、エンドポイントをcatch. 例えば:

MyServiceClient client = null;
try
{
    client = new MyServiceClient();
}
catch (InvalidOperationException)
{
    EndpointAddress defaultAddress = new EndpointAddress(...);
    Binding defaultBinding = new Binding(...);
    client = new MyServiceClient(defaultBinding, defaultAddress);
}
于 2011-04-07T16:29:27.073 に答える
2

以下のように構成セクションを取得できます。

var clientSection =  System.Configuration.ConfigurationManager.GetSection("system.serviceModel/client");

値が null の場合、またはclientSection.Endpoints要素が含まれていない場合は、定義されていません。

于 2011-04-07T16:32:00.390 に答える
2

app.config ファイルを使用せずにスタンドアロン サービス クライアントを実装しようとしたときに、同じ種類の問題に直面しました。そしてついに私はそれを整理することができました。以下のコードサンプルに従ってください。それは正常に動作しており、私はそれをテストしました。

BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "BasicHttpBinding_ITaskService";
binding.CloseTimeout = TimeSpan.FromMinutes(1);
binding.OpenTimeout = TimeSpan.FromMinutes(1);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.SendTimeout = TimeSpan.FromMinutes(1);
binding.AllowCookies = false;
binding.BypassProxyOnLocal = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferSize = 65536;
binding.MaxBufferPoolSize = 524288;
binding.MaxReceivedMessageSize = 65536;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = true;
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm = "";
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;

Uri endPointAddress = new Uri("http://www.kapanbipan.com/TaskService.svc");
ChannelFactory<taskmgr.TaskService.ITaskServiceChannel> factory = new ChannelFactory<ITaskServiceChannel>(binding, endPointAddress.ToString());
taskmgr.TaskService.ITaskServiceChannel client = factory.CreateChannel();
于 2012-10-13T17:27:30.260 に答える
1

これを試してみてください...テストされていませんが、うまくいくはずです。一致するコントラクトを持つエンドポイントの構成がチェックされます。名前で一致するように変更したり、別の情報を返したり、状況に合ったものに変更したりできます。一致するものが見つからない場合は、ロジックを挿入してデフォルトのエンドポイントを作成できます。

    public List<EndpointAddress> GetEndpointAddresses(Type t)
    {
        string contractName = t.FullName;
        List<EndpointAddress> endpointAddresses = new List<EndpointAddress>();
        ServicesSection servicesSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;

        foreach (ServiceElement service in servicesSection.Services)
        {
            foreach (ServiceEndpointElement endpoint in service.Endpoints)
            {
                if (string.Compare(endpoint.Contract, contractName) == 0)
                {
                    endpointAddresses.Add(new EndpointAddress(endpoint.Address));
                }
            }
        }

        if (endpointAddresses.Count == 0)
        {
            //TODO: Add logic to determine default
            endpointAddresses.Add(new EndpointAddress("Your default here"));
        }

        return endpointAddresses;
    }
于 2011-04-07T18:56:13.957 に答える