如何从另一台计算机访问WCF(控制台应用程序)服务 [英] How to access a WCF (Console application) service from another computer

查看:64
本文介绍了如何从另一台计算机访问WCF(控制台应用程序)服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个非常简单的解决方案有3个项目:WcfClient,WcfCommon,WcfService1:



WcfCommon:





 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.ServiceModel;

命名空间 WcfCommon
{

[ServiceContract]
public interface IServiceList
{
[OperationContract]
void run( string id, int n);

[OperationContract]
int getResult( string id);
}
}





WcfService1:

 命名空间 WcfService1 
{
public class ServiceList:IServiceList
{
Dictionary< string,TestService> SL;

public ServiceList()
{
sl = new Dictionary< string,TestService>();
Console.WriteLine( ServiceList:实例化服务列表);
}

public void run( string id, int input)
{
if (!sl.ContainsKey(id))
{
sl.Add(id, new TestService(id));
}

sl [id] .factorial(输入);
}

public int getResult( string id)
{
if (sl.ContainsKey(id))
{
Console.WriteLine( {0} - getResult:返回{1},id ,sl [id] .dec.fac);
return sl [id] .dec.fac;
}
Console.WriteLine( 找不到ID + id) ;
return 0 ;
}
}


public class TestService
{
public 决策dec;
public string ticker;

public TestService( string id)
{
dec = new Decision();
ticker = id;
Console.WriteLine( TestService:创建id + id);
}

~TestService()
{
Console.WriteLine( TestService:销毁id + ticker);
}

public int factorial( int n)
{
dec.fac = 1 ;
for int i = 1 ; i < = n; i ++)dec.fac * = i;
Console.WriteLine( {2}:计算因子{0} ... {1},n,dec.fac,ticker);
return dec.fac;
}
}
}





WcfService1的主要Program.cs是:



 命名空间 WcfService1 
{
class 计划
{
静态 void Main( string [] args)
{
// 步骤1创建一个URI作为基地址。
Uri baseAddress = new Uri( http://192.168.0.5/GettingStarted/);

// 步骤2创建ServiceHost实例
ServiceHost selfHost = new ServiceHost( typeof (ServiceList),baseAddress);

尝试
{


// 步骤4启用元数据交换。
ServiceMetadataBehavior smb = new ServiceMetadataBehavior( );
smb.HttpGetEnabled = true ;

selfHost.Description.Behaviors.Add(smb);
selfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,MetadataExchangeBindings.CreateMexHttpBinding(), mex) ;

// 步骤3添加服务端点。
selfHost .AddServiceEndpoint( typeof (IServiceList), new / * BasicHttpBinding()* / WSHttpBinding(), ServiceList);

// 步骤5启动服务。
selfHost。打开();
Console.WriteLine( 服务准备就绪。);
Console.WriteLine( 按< ENTER>终止服务。);
Console.WriteLine();
Console.ReadLine();

// 关闭ServiceHostBase以关闭服务。
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine( 发生异常:{0},ce.Message);
selfHost.Abort();
}
}
}

}





客户端是非常简单的wcfClient:



 命名空间 WcfClient 

{
class 计划
{
静态 void Main( string [] args)
{
wcfClientTest test = new wcfClientTest();


for int i = 1 ; i < 5 ; i ++)
{
test.testServ.run( test,i);
Console.WriteLine( Factorial {0} ... {1},i ,test.testServ.getResult( test));
}

for int i = 1 ; i < 5 ; i ++)
{
test.testServ.run( cooc,i);
Console.WriteLine( Factorial {0} ... {1},i ,test.testServ.getResult( cooc));
}

Console.WriteLine( 按< ENTER>退出客户端。);
Console.ReadLine();
}
}

public class wcfClientTest
{
public IServiceList testServ;

public wcfClientTest()
{
WSHttpBinding myBinding = new WSHttpBinding();
// BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress epa = < span class =code-keyword> new
EndpointAddress( http://192.168.0.5/GettingStarted / ServiceList);

ChannelFactory< IServiceList> cf = new ChannelFactory< IServiceList>(myBinding,epa);

testServ = cf.CreateChannel();
Console.WriteLine( wcfClientTest:实例化新实例);
}
}
}





如果我同时运行客户端和同一台机器上的服务器。



但如果我尝试在同一局域网内的另一台机器上运行客户端(比如192.168.0.7),客户端无法连接服务。有什么想法吗?





谢谢!

解决方案

如果是WCF服务已经托管在IIS服务器上....然后你需要用端口号命中该机器的IP地址。它已经在URL中托管.....





希望,这会给你一些想法....

have a very simple solution with 3 projects: WcfClient, WcfCommon, WcfService1:

WcfCommon:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace WcfCommon
{

    [ServiceContract]
    public interface IServiceList
    {
        [OperationContract]
        void run(string id, int n);

        [OperationContract]
        int getResult(string id);
    }
}



WcfService1:

namespace WcfService1
{
    public class ServiceList : IServiceList
    {
        Dictionary<string, TestService> sl;

        public ServiceList()
        {
            sl = new Dictionary<string, TestService>();
            Console.WriteLine("ServiceList: Instantiating the service list");
        }

        public void run(string id, int input)
        {
            if ( !sl.ContainsKey(id) )
            {
                sl.Add(id, new TestService(id));
            }

            sl[id].factorial(input);    
        }

        public int getResult(string id) 
        {
            if (sl.ContainsKey(id))
            {
                Console.WriteLine("{0}--getResult: Returning {1}", id, sl[id].dec.fac);
                return sl[id].dec.fac;
            }
            Console.WriteLine("Could not find id " + id);
            return 0;
        }
    }


public class TestService
    {
        public Decision dec;
        public string ticker;

        public TestService(string id)
        {
            dec = new Decision();
            ticker = id;
            Console.WriteLine("TestService: Creating id " + id);
        }

        ~TestService()
        {
            Console.WriteLine("TestService: Destroying id " + ticker);
        }

        public int factorial(int n)
        {
            dec.fac = 1;
            for (int i = 1; i <= n; i++) dec.fac *= i;
            Console.WriteLine("{2}: Computing factorial {0}... {1}", n, dec.fac, ticker);
            return dec.fac;
        }
    }
}



The main Program.cs for WcfService1 is:

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

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

            try
            {
                

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

                selfHost.Description.Behaviors.Add(smb);
                selfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

                // Step 3 Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(IServiceList), new /*BasicHttpBinding()*/ WSHttpBinding(), "ServiceList");
                
                // 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();
            }
        }
    }

}



The client is very simple wcfClient:

namespace WcfClient

{
    class Program
    {
        static void Main(string[] args)
        {
            wcfClientTest test = new wcfClientTest();

            
            for (int i = 1; i < 5; i++)
            {
                test.testServ.run("test", i);
                Console.WriteLine("Factorial {0}... {1}", i, test.testServ.getResult("test"));
            }

            for (int i = 1; i < 5; i++)
            {
                test.testServ.run("cooc", i);
                Console.WriteLine("Factorial {0}... {1}", i, test.testServ.getResult("cooc"));
            }

            Console.WriteLine("Press <ENTER> to exit client.");
            Console.ReadLine();
        }
    }

    public class wcfClientTest
    {
        public IServiceList testServ;

        public wcfClientTest()
        {
            WSHttpBinding myBinding = new WSHttpBinding();
            //BasicHttpBinding myBinding = new BasicHttpBinding();
            EndpointAddress epa = new EndpointAddress("http://192.168.0.5/GettingStarted/ServiceList");

            ChannelFactory<IServiceList> cf = new ChannelFactory<IServiceList>(myBinding, epa);

            testServ = cf.CreateChannel();
            Console.WriteLine("wcfClientTest: Instantiating a new instance");
        }
    }
}



The whole thing works well if I run both the client and the server on the same machine.

But if I try to run the client on a different machine within the same LAN (say 192.168.0.7), the client cannot connect to the service. Any ideas?


Thanks!

解决方案

If the WCF service has been hosted on the IIS server.... then u need to hit the IP address of that machine with the port no. on which it has been hosted in the URL.....


Hope, this will gives you some idea....


这篇关于如何从另一台计算机访问WCF(控制台应用程序)服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆