1

jQueryを使用して、またはブラウザーを介してメソッドをヒットしようとすると、404が表示されます。構成は次のようになります。

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="RegistrationBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="RegistrationEndpointBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
        <service name="Corp.EmployeeService" behaviorConfiguration="RegistrationBehavior">
            <endpoint address="" binding="webHttpBinding" contract="Corp.IEmployeeService" behaviorConfiguration="RegistrationEndpointBehavior" />
        </service>
    </services>
</system.serviceModel>

私のメソッドはOperationContractとWebInvoke属性で装飾されているので、これは構成と関係があると思います。

私のインターフェース(投稿用に簡略化)は次のようになります。

namespace Corp
{
    [ServiceContract]
    public interface IEmployeeService
    {
        [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "POST")]
        int Register(string username);
    }
}

サービスクラス自体に、次の属性で装飾しました。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeeService : IEmployeeService

私が述べたように、私はhttp://localhost/EmployeeService.svc/Registerを使用して、または次のようにjQueryを使用してサービスをヒットしようとしました:

$.ajax({
    cache: false,
    async: true,
    type: "POST",
    dataType: "json",
    url: "http://localhost/EmployeeService.svc/Register",
    data: '{"username":"test"}',
    contentType: "application/json;charset=utf-8",
    success: function (msg) {
        var status = msg.RegisterResult;
        if (status == 1) {
            alert('Success');
        } else {
            alert('Failure');
        }
    },
    error: function (msg) {
        alert('Exception - ' + msg);
    }
});

どちらも404を返します。

私は共有ホスティング会社でこのサービスをホストしているので、以下に示すようにServiceFactoryで自分を作成しました。

public class IisServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var newAddress = baseAddresses[0];

        if (!newAddress.AbsoluteUri.Contains("http://www."))
            newAddress = new Uri(newAddress.AbsoluteUri.Replace("http://", "http://www."));

        var webServiceHost = new IisServiceHost(serviceType, newAddress);
        return webServiceHost;
    }
}

public class IisServiceHost : ServiceHost
{
    public IisServiceHost(Type serviceType, params Uri[] baseAddresses): base(serviceType, baseAddresses) { }
}

jQueryからPOSTをヒットしようとすると、POSTが発生します。(テスト用に簡略化された別のサンプルサービスからの)404応答を以下に示します。

404同じプロジェクト内の別のサービスからの応答

この例外のテキストは次のとおりです。HTTP404。探しているリソース(またはその依存関係の1つ)が削除されたか、名前が変更されたか、一時的に使用できなくなった可能性があります。次のURLを確認し、スペルが正しいことを確認してください。

4

0 に答える 0