1

私はこのインターフェースを持っています:

  [ServiceContract]
    public interface ILocationService
    {
        [OperationContract]
        bool RegisterForNotification(string name, string uri);

        [OperationContract]
        bool UnRegisterForNotification(string name);
    }

そしてこのサービス:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class LocationBasedService : ILocationService
    {
        #region Registrations
        public bool RegisterForNotification(string name, string uri)
        {
            return true;
        }

        public bool UnRegisterForNotification(string name)
        {
            return true;
        }     
        #endregion
    }

および次の構成:

<configuration>
  <system.serviceModel>
    <services>
      <service name="PushNotifications.Server.Service.LocationBasedService" >
        <endpoint address="http://localhost:8000/LocationBasedService"
                 binding="basicHttpBinding"                 
                 contract="Services.Interface.ILocationService"/>
      </service>      
    </services>
  </system.serviceModel>

ServiceHost を使用して WPF アプリケーションで自己ホストされます。このコードは次のようになります。

private void startSrv_Click(object sender, RoutedEventArgs e)
        {
            try 
            {
                host = new ServiceHost(typeof(LocationBasedService));
                host.Open();
                AddDiagnosticMessage("service successfully initialized");
                AddDiagnosticMessage(string.Format("{0} is up and running with these end points", host.Description.ServiceType));
                foreach (var se in host.Description.Endpoints)
                    AddDiagnosticMessage(se.Address.ToString());
            }
            catch (TimeoutException ex)
            {
                AddDiagnosticMessage(string.Format("The service operation timeod out. {0}", ex));
            }
            catch (CommunicationException ex)
            {
                AddDiagnosticMessage(string.Format("Could not start host service. {0}", ex));
            }
            catch (Exception ex)
            {
                AddDiagnosticMessage(ex.Message);
            }                  
        }

サービスは例外なく開始されます。しかし、URL http://localhost:8000/LocationBasedServiceをブラウザに送信すると、HTTP 400 Bad Request が返されます。Visual Studio の [サービス参照の追加] を使用して WCF クライアントを作成しようとすると、次のエラーが発生します。

「http://localhost:8000/LocationBasedService」。コンテンツ タイプ application/soap+xml; charset=utf-8 はサービスhttp://localhost:8000/LocationBasedServiceでサポートされていませんでした。クライアントとサービスのバインディングが一致していない可能性があります。リモート サーバーがエラーを返しました: (415) コンテンツ タイプが 'application/soap+xml; であるため、メッセージを処理できません。charset=utf-8' は予期されたタイプの 'text/xml ではありませんでした。charset=utf-8'.. サービスが現在のソリューションで定義されている場合は、ソリューションを構築してサービス参照を再度追加してみてください。

次のコードを使用してクライアントを呼び出そうとすると、タイムアウト例外が発生します。

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            statusMessages.Add(GetFormattedMessage("initiailing client proxy"));
            var ep = new EndpointAddress("http://localhost:8000/LocationBasedService");
                     var proxy = ChannelFactory<ILocationService>.CreateChannel(new BasicHttpBinding(), ep);          
            var register = proxy.RegisterForNotification("name1", @"http://google.com");
            if (register)
            {  Console.Writeline(register.ToString()); }
        }

誰かが私が見逃したものについていくつかの洞察を与えることができますか. これは簡単な演習になるはずでした:!

ティア。

4

1 に答える 1

2

Bad HTTP リクエストは、次のコードを追加することで解決されました。

 host = new ServiceHost(typeof(LocationBasedService), new Uri("http://localhost:8000/LocationBasedService"));

  // Enable metadata publishing.
   ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
   smb.HttpGetEnabled = true;
   smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
   host.Description.Behaviors.Add(smb);

    host.Open();   

サービスのタイムアウトに関しては、これは私のパーソナル ファイアウォールが原因でした。許可されたアプリケーションのリストに Visual Studio を追加する必要がありました。

ビジネスに戻ります。

于 2011-12-07T11:56:00.843 に答える