0

WCF サービスを自己ホストするコンソール アプリがあります。ユーザーが asp.net アプリケーションにアクセスしてページのボタンをクリックしたときに、この自己ホスト WCF サービス (ローカルでホストされているサービス) を呼び出すスクリプトを作成する方法。私のスクリプトに何か問題があると思います。助けてください。

namespace SelfHost
{
  [ServiceContract]
  public interface IHelloWorldService
  {
      [OperationContract]
      string SayHello(string name);
  }

  [AspNetCompatibilityRequirements(RequirementsMode =     AspNetCompatibilityRequirementsMode.Allowed)]
  public class HelloWorldService : IHelloWorldService
  {
    public string SayHello(string name)
    {
        return string.Format("Hello, {0}", name);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Uri baseAddress = new Uri("http://127.0.0.1/hello");

        // Create the ServiceHost.
        using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
        {
            // Enable metadata publishing.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            // Open the ServiceHost to start listening for messages. Since
            // no endpoints are explicitly configured, the runtime will create
            // one endpoint per base address for each service contract implemented
            // by the service.
            host.Open();

            Console.WriteLine("The service is ready at {0}", baseAddress);
            Console.WriteLine("Press <Enter> to stop the service.");
            Console.ReadLine();

            // Close the ServiceHost.
            host.Close();
        }
    }
}

サービスを呼び出すスクリプト

<script type="text/javascript">
    function invokeService() {
        $(document).ready(function () {
            var userName = " test";

            $.ajax({
                type: "POST",
                async: "false",
                url: "http://127.0.0.1:8080/hello",
                data: "{'name':'" + userName + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                processData: true,
                method: "SayHello",
                success: function (result) {
                    AjaxSucceeded(result);
                },
                error: function (retult) {
                    AjaxFailed(result);
                }
            });
        });
    }
4

2 に答える 2

-1

スクリプトによるクロスドメイン呼び出しが許可されていないことがわかりました。別の方法は、wcf サービスを呼び出すためのコマンド ライン アプリまたはコンソール ファイルを用意することです。次に、このアプリをレジストリに登録します。次に、スクリプトからアプリを起動します。

于 2013-06-20T04:29:29.677 に答える