1

この例に従って動作させようとしています: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx

これが私のものApp.configです:

<system.serviceModel>
   <services>      
      <!-- The service for the TEST WEB client -->
      <service name="MyServer.AAServiceType" behaviorConfiguration="Default">
         <endpoint address="testservice" 
                   binding="webHttpBinding" behaviorConfiguration="webBehavior"
                   contract="MyServer.AAIContractName" />
         <host>
            <baseAddresses>
               <add baseAddress="http://localhost:8787/" />
            </baseAddresses>
         </host>
      </service>
    </services>
    <behaviors>
       <serviceBehaviors>
          <!-- TEST WEB BEHAVIOR -->
          <behavior name="Default">
             <serviceMetadata httpGetEnabled="true"/>
          </behavior>
       </serviceBehaviors>
       <!-- TEST WEB ENDPOINT -->
       <endpointBehaviors>
          <behavior name="webBehavior">
             <webHttp />
          </behavior>
       </endpointBehaviors>
   </behaviors>
</system.serviceModel>

更新:サービス契約は次のとおりです。

namespace MyServer
{
    [ServiceContract(SessionMode=SessionMode.NotAllowed)] 
    public interface IContractName 
    { 
        [WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)] 
        [OperationContract] 
        string GetDate(string day, string month, string year); 
    }

    public class ServiceType : IContractName 
    {  
        public string GetDate(string day, string month, string year) 
        { 
           return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy"); 
        } 
    }
}

問題は、8787 ポートに (puttyたとえば を使用して) 接続しようとすると、「接続が拒否されました」というエラーが返されることです。ご覧のとおり、コントラクト クラスとサービスの実装に間違った名前を付けようとしましたが、例外はありませんでした。私は何を間違っていますか?

4

2 に答える 2

1

サービスを起動するコードにエラーがありました。正しいコードは次のとおりです。

using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceType)))
{
    try
    {
      // Open the ServiceHost to start listening for messages.
      serviceHost.Open();

      // The service can now be accessed.
      Console.WriteLine("The service is ready.");
      Console.WriteLine("Press <ENTER> to terminate service.");
      Console.ReadLine();

      // Close the ServiceHost.
      serviceHost.Close();
    }
    catch (CommunicationException commProblem)
    {
      Console.WriteLine(commProblem.Message);
      Console.ReadLine();
    }
}
于 2010-09-21T16:00:57.867 に答える
1

IIS でホスティングしていますか、それとも自己ホスティングですか??

これを IIS でホストしている場合 (*.svc ファイルを使用)、IIS がアドレスを指示します。

http://yourserver/yourvirtualdirectory/yourservice.svc/.........

自己ホストの場合、すべて問題ないように思えます。その場合、ベース アドレスが影響します。

http://localhost:8787/testservice

が緊急対応用住所になっているはずです。

于 2010-09-21T15:37:52.747 に答える