3

私は .net を初めて使用し、WCF についてほとんど知りません。私のコードが明示的にスレッドを生成しない場合、WCF が SELF-HOST シナリオで同時呼び出しをどのように処理するのか疑問に思っています。そのため、stackoverflow をよく読んだ後、テスト アプリを作成しましたが、機能していないようです。お知らせ下さい。どうもありがとう。

ご注意ください ...

  1. 私の質問は WCF SELF HOSTINGについてのみです。そのため、IIS 関連は参照しないでください。
  2. 私はwebHttpBindingを使用しています。
  3. maxConnection と service throttling 設定があることは理解していますが、私の研究セットアップでは2 つの同時呼び出しにしか興味がありません。したがって、最大接続数やスレッド プールに関する懸念はありません。
  4. 私のテストサービスはセッションを使用していません。

以下のようなコード...

namespace myApp
{
  [ServiceContract(SessionMode = SessionMode.NotAllowed)]
  public interface ITestService
  {
    [OperationContract]
    [WebGet(UriTemplate="test?id={id}")]
    string Test(int id);
  }

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, 
                   ConcurrencyMode = ConcurrencyMode.Multiple)]
  public class TestService : ITestService
  {
    private static ManualResetEvent done = new ManualResetEvent(false);

    public string Test(int id)
    {
      if (id == 1)
      {
        done.Reset();
        done.WaitOne();
      }
      else
      {
        done.Set();
      }
    }
  } 
}

app.config ...

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name = "TestEndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name = "myApp.TestService">
        <endpoint address = "" behaviorConfiguration="TestEndpointBehavior"
                  binding = "webHttpBinding"
                  contract = "myApp.ITestService">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/test/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <sessionState mode = "Off" />
  </system.web>

私がテストした方法...

アプリケーションを実行したら、ブラウザーを開きました。場合によっては FF で、http://localhost:8080/test/test?id=1を 1 回呼び出しました。この要求により、アプリは信号待ち (WaitOne など) を一時停止します。次に、別のブラウザー タブで別の呼び出しをhttp://localhost:8080/test/test?id=2に行いました。予想されるのは、このリクエストがシグナルを設定するため、サーバーが両方のリクエストに対して返されることです。

しかし、アプリがハングし、2 番目の要求で Test 関数が入力されませんでした。どうやら私のコードは同時/同時呼び出しをサポートしていません。何か間違っていますか?

4

1 に答える 1

0

単一のクラスを使用して、wcf サービスをセットアップし、インターフェイスを破棄できます。global.asax ファイルも追加する必要があります。2 番目の呼び出しを行った後、それらはすべて「終了」を返します。

この構成は、あなたが望むことを行います。次を使用して TestService.cs を作成します。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
             ConcurrencyMode = ConcurrencyMode.Multiple)]
[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public class TestService
{
    private static ManualResetEvent done = new ManualResetEvent(false);

    [OperationContract]
    [WebGet(UriTemplate = "test?id={id}")]
    public string Test(int id)
    {
        if (id == 1)
        {
            done.Reset();
            done.WaitOne();
        }
        else
        {
            done.Set();
        }
        return "finished";
    }


}

web.config:

<configuration>
<system.web>
  <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
 <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<system.serviceModel>
<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="false"  >    </standardEndpoint>


  </webHttpEndpoint>
</standardEndpoints>
<behaviors>
  <serviceBehaviors>
    <behavior>

      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>

Global.asax ファイル:

public class Global : System.Web.HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("testservice", new WebServiceHostFactory(), typeof(TestService)));
    }
}
于 2012-04-03T15:17:32.470 に答える