WCF - 从客户端代码设置端点

时间:2016-03-26 11:44:32

标签: c# .net wcf communication

我只是在学习WCF,我想编写应用程序,其中客户端位于不同于主机的PC上。我首先在同一台PC上编写了客户端和服务器,它工作正常(msdn +上的教程),这里是si代码:

服务

IService1.cs

namespace GettingStartedLib
{
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
}

Service1.cs

namespace GettingStartedLib
{
    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            // Code added to write output to the console window.
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
    }
}

主机   - 我添加了所有必要的参考资料。

namespace GettingStartedHost
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1 Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");

            // Step 2 Create a ServiceHost instance
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            try
            {
                // Step 3 Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");

                // Step 4 Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5 Start 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();
            }
        }
    }
}

最后是客户

namespace GettingStartedClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step 1: Create an instance of the WCF proxy.
            CalculatorClient client = new CalculatorClient();
            Console.ReadLine();
            // 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);

            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

            //Step 3: Closing the client gracefully closes the connection and cleans up resources.
            client.Close();

            Console.ReadLine();
        }
    }
}

我也添加了服务参考(添加服务参考 - 发现 - 选择并确定)。

App.config中:

<?xml version="1.0"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICalculator" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8733/Design_Time_Addresses/GettingStartedLib/Service1/"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICalculator"
                contract="ServiceReference1.ICalculator" name="BasicHttpBinding_ICalculator" />
        </client>
    </system.serviceModel>
</configuration>

我有点困惑,因为在app.config中是不同的地址,比我在主机Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");中设置的那样,但是它有效,所以没问题......

现在我想在运行的客户端应用程序中设置客户端端点,所以首先我想写一些简单的东西,所以我尝试这个(从另一个stackoverflow页面):

namespace GettingStartedClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step 1: Create an instance of the WCF proxy.
            Console.ReadLine();

            EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
            Uri uri = new Uri("http://localhost:8733/Design_Time_Addresses/GettingStartedLib/Service1/");
            var address = new EndpointAddress(uri, spn);
            var client = new CalculatorClient("WSHttpBinding_IEchoService", address);

            // 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);

            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

            //Step 3: Closing the client gracefully closes the connection and cleans up resources.
            client.Close();

            Console.ReadLine();
        }
    }
}

....但是它掉了下来。有人可以帮我设置它吗?在这个项目的最终版本中,我想拥有客户端应用程序,其中客户端设置服务器的IP,然后可以从服务器(主机)调用该操作。

3 个答案:

答案 0 :(得分:0)

请更新托管服务的计算机的服务器名称/ IP地址:

例如你配置:

                     

您的代码:Uri baseAddress = new Uri“http:// {ServerName / Ip-Address} / GettingStarted /

答案 1 :(得分:0)

我自己解决了。这是我的解决方案。首先,我必须将localhost更改为我的IP和主机中的Http绑定到basicHttpBindig:

<table id="T1" border='1'>
  <thead>
    <tr>
      <th>First Name</th>
      <th>Middle Name</th>
      <th>Last Name</th>
      <th>Suffix</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>12</td>
      <td>34</td>
      <td>56</td>
      <td>78</td>
    </tr>
  </tbody>
</table>

<table id="T2" border='1'>
  <thead>
    <tr>
      <th>First Name</th>
      <th>Middle Name</th>
      <th>Last Name</th>
      <th>Suffix</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>12</td>
      <td><a href="#" data-toggle="modal" data-target="#new-modal">34</a></td>
      <td>56</td>
      <td>23</td>
    </tr>
    <tr>
      <td>bat</td>
      <td><a href="#" data-toggle="modal" data-target="#new-modal">man</a></td>
      <td>11</td>
      <td>21212</td>
    </tr>
    <tr>
      <td>james</td>
      <td><a href="#" data-toggle="modal" data-target="#new-modal">bond</a></td>
      <td>007</td>
      <td>dadadada</td>
    </tr>
    <tr>
      <td>12</td>
      <td><a href="#" data-toggle="modal" data-target="#new-modal">34</a></td>
      <td>56</td>
      <td>78</td>
    </tr>
  </tbody>
</table>
<br /><br /><br /><br />

<button id="btn">color if matches</button>

然后我不得不从客户端删除我的服务引用并为这个新主机添加新的服务引用,所以我的App.config更改为:

   selfHost.AddServiceEndpoint(typeof(ICalculator), new BasicHttpBinding(), "CalculatorService");

最后我在客户端更改了这个:

<?xml version="1.0"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICalculator" />

            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://ip:8000/PokusWPF/CalculatorService"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICalculator"
                contract="ServiceReference1.ICalculator" name="BasicHttpBinding_ICalculator" />
        </client>
    </system.serviceModel>
</configuration>

现在一切都在不同的PC上运行。我希望它会对某人有所帮助。

答案 2 :(得分:0)

请通过局域网或互联网验证远程/客户端PC是否处于正确的网络中,首先通过Web浏览器(如Internet Explorer等)在客户端PC上打开来验证您的服务。当它在任何Web浏览器中开始工作时,它将会也适用于客户端应用程序。

验证您的托管服务,尝试在IIS等中托管相同的服务,如果它是自托管的,请确保端口正确并且远程请求使用正确的端口。