1

クラス GreetService のメソッドを公開する WCF REST サービスがあります。

[ServiceContract]
public class GreetService
{
    [WebGet(UriTemplate = "greet/{name}")]
    public String GreetName(string name)
    {
        return "Hello " + name;
    }
}

また、Global.asax にルートを登録しました。

RouteTable.Routes.Add(new ServiceRoute("GreetService", new WebServiceHostFactory(), typeof(GreetService)));

これをビジュアル スタジオから直接実行すると、UriTemplate を利用して、 http://localhost:5432/GreetService/greet/JohnDoeへの GET 呼び出しを使用してこのメ​​ソッドを呼び出すことができ ます。

ただし、Greet.svc ファイルを作成してこれを IIS7 に展開した後、次の動作を観察しています。

WebGetAttribute が IIS で機能しない理由はありますか? または、私が間違っていることは他にありますか?

編集:これは、IIS が使用するディレクトリにある web.config ファイルの ServiceModel 部分です。

<system.serviceModel>
    <!-- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> -->
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
</system.serviceModel>

編集2:完全を期すために、ここに私の完全なweb.configファイルがあります:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
           type="System.Web.Routing.UrlRoutingModule,
           System.Web, Version=4.0.0.0,
           Culture=neutral,
           PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler"
         preCondition="integratedMode"
         verb="*" path="UrlRouting.axd"
         type="System.Web.HttpForbiddenHandler, 
         System.Web, Version=4.0.0.0, Culture=neutral, 
         PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
  </system.webServer>

  <system.serviceModel>
     <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>--> 
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>
4

4 に答える 4

1

ルートを次のように定義した場合:

new ServiceRoute("GreetService", .....

次に、サービスを呼び出すことができるはずです

http://localhost:5432/YourVirtualDirectory/GreetService/greet/JohnDoe

また、Web アプリが (仮想ディレクトリではなく) IIS ルートにデプロイされている場合は、次のようになります。

http://localhost:5432/GreetService/greet/JohnDoe

ServiceRoute を定義するときGreet.svc、実際にはファイルを指定する必要がなくなります。ServiceRouteエントリには、IIS がサービスをインスタンス化して呼び出すために必要なすべての情報が既に含まれています。URL に *.svc ファイルを含める必要はありません ( svc ファイルには、基本的にServiceRouteエントリと同じ情報が含まれています)。

于 2010-07-12T18:31:47.690 に答える
1

Change the line in your global.asax.cs to read:

RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(GreetService)));

and put the following in your web.config right under the root <configuration> node:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
           type="System.Web.Routing.UrlRoutingModule, 
          System.Web.Routing, Version=4.0.0.0, 
          Culture=neutral, 
          PublicKeyToken=31BF3856AD364E35" />

    </modules>
    <handlers>
      <add name="UrlRoutingHandler"
         preCondition="integratedMode"
         verb="*" path="UrlRouting.axd"
         type="System.Web.HttpForbiddenHandler, 
         System.Web, Version=4.0.0.0, Culture=neutral, 
         PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
  </system.webServer>

(do make sure you're using the right .NET version) and see what that does for you.

于 2010-07-12T19:51:00.373 に答える
1

注: web.config、少なくとも system.servicemodel 部分を投稿してください。

通常、両方ではなく、ルートベースの構成または .svc ファイルのいずれかを使用しますが、それは問題とは直交しています。FWIW、サービスを機能させてルートを使用すると、.svcファイルを強制終了できるはずです。

WSDL を生成して呼び出すことができるので、エンドポイントの動作として webhttp がないように思えますか?

このようにエンドポイントの動作が定義されていることを確認してください (もちろん差分名でもかまいません)。

    <endpointBehaviors> 
        <behavior name="webHttpBehavior"> 
            <webHttp /> 
        </behavior> 
    </endpointBehaviors> 

次に、サービス エンドポイントに behaviorConfiguration="webHttpBehavior" が含まれていることを確認します。

于 2010-07-13T16:19:23.703 に答える
0

問題は、マシン構成に次のセクションがないことです

<configSections>
    <sectionGroup name="system.serviceModel" type="System.ServiceModel.Configuration.ServiceModelSectionGroup, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <section name="standardEndpoints" type="System.ServiceModel.Configuration.StandardEndpointsSection, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </sectionGroup>
</configSections>

これを web.config の上 ( の開始タグの後<configuration>) に追加すると、この問題が解決するはずです。

于 2012-07-11T02:58:40.290 に答える