1

私は WCF を初めて使用し、2 つのプロジェクトで 1 つのソリューションを作成しました。1 つはクライアント アプリで、もう 1 つは Web サービスです。Web サービス コードをコンパイルすると、エラーなしで実行されます。

namespace Microsoft.ServiceModel.Samples
{

class Program
{
    static void Main(string[] args)
    {

        // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
        Uri baseAddress = new Uri("http://localhost:8888/ServiceModelSamples/Service");

        /* Step 2 of the hosting procedure: Create ServiceHost
         * Use the ServiceHost class to configure and expose a service for use by client applications when you are not using Internet Information Services (IIS) to expose a service.  
         * IIS interacts with a ServiceHost object on your behalf.             
         */
        ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

        try
        {
            //WSHttpBinding is an interoperable binding that supports distributed transactions and secure, reliable sessions.
            WSHttpBinding ws = new WSHttpBinding();
            ws.Security.Mode = SecurityMode.Message; //Use SOAP message security
            ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows; //Use windows authentication

            // Step 3 of the hosting procedure: Add a service endpoint.
            // Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address.
            //      The contract is the definition of what functionality the web service offers (i.e. its API)
            //      The binding specifies how the service communicates (protocols, transports, and message encoders)
            //      The address is the name of the endpoint being added to this service host
            selfHost.AddServiceEndpoint(
                typeof(ICalculator),
                ws, 
                "CalculatorService");

            // Step 4 of the hosting procedure: Enable metadata exchange.
            // Controls the publication of service metadata and associated information.
            //      HttpGetEnabled indicates whether to publish service metadata for retrieval using an HTTP/GET request.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            //add the service metadata behavior to the list of service host behaviors
            selfHost.Description.Behaviors.Add(smb);

            // Step 5 of the hosting procedure: Start (and then stop) the service.
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("An exception occurred: {0}", ce.Message);
            selfHost.Abort();
        }

        Console.ReadLine();
    }
}

クライアント側では、コードは client.add 行で「http://localhost:8000/ServiceModelSamples/Serviceメッセージを受け入れることができるエンドポイントがリッスンしていませんでした」で失敗し、「ターゲットマシンがアクティブに拒否したため、接続を確立できませんでした」という内部例外があります 127.0.0.1 :8000"

//Step 1: Create an endpoint address and an instance of the WCF Client.
CalculatorClient client = new CalculatorClient();

// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

私の app.config は次のようになります。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ICalculator" />
            </wsHttpBinding>
        </bindings>
        <client>
                <endpoint address="http://localhost:8000/ServiceModelSamples/Service"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
                contract="CalculatorServiceReference.ICalculator" name="WSHttpBinding_ICalculator">

            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

専門家は私を正しい方向に向けることができますか? さまざまなエンドポイント アドレスを試しましたが、うまくいきません。前もって感謝します!

4

2 に答える 2

3

クライアントがポート 8000 に接続しようとしている間、サーバーは 8888 でホストされています。一致するようにサーバーまたはクライアントのポートを変更してください。

8888 ではなく 8000 にしたいようです。

Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
于 2013-09-17T18:51:52.843 に答える
0

私はあなたのコードをテストしました。次のように、クライアント app.config ファイルのベースアドレスにサービスを追加する必要があると思います

エンドポイント アドレス="http://localhost:8888/ServiceModelSamples/Service/CalculatorService"

于 2013-09-17T20:16:07.303 に答える