我正在使用Visual Studio 2015和C#。
对于我的服务器: 我有一个控制台应用程序,在Program.cs中包含以下代码:
const string serviceURL = "http://localhost:1234/AccountService";
ServiceHost serviceHost = new ServiceHost(
typeof(AccountService),
new Uri(serviceURL));
serviceHost.AddServiceEndpoint(
typeof(IAccountService),
new BasicHttpBinding(BasicHttpSecurityMode.None),
"");
serviceHost.Open();
服务的接口位于共享的类库中:
using Common.Types;
using System.ServiceModel;
namespace Common.Interfaces
{
[ServiceContract]
[ServiceKnownType(typeof(UserAccountResult))]
public interface IAccountService
{
[OperationContract]
UserAccountResult Login(string userName, string password);
[OperationContract]
UserAccountResult CreateAccount(string userName, string password);
}
}
UserAccountResult是一个枚举
服务器的控制台应用程序也具有接口的实现:
using Common.Types;
namespace Server
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class AccountService : IAccountService
{
public UserAccountResult CreateAccount(string userName, string password)
{
return UserAccountResult.UserNameTaken;
}
public UserAccountResult Login(string userName, string password)
{
return UserAccountResult.CannotConnectToServer;
}
}
}
对于客户:
const string serviceURL = "http://192.168.6.139:1234/AccountService";
ChannelFactory<IAccountService> channelFactory =
new ChannelFactory<IAccountService>(
new BasicHttpBinding(BasicHttpSecurityMode.None),
new EndpointAddress(serviceURL));
IAccountService myService = channelFactory.CreateChannel();
现在,服务器exe正在VMware Workstation 12 Player(使用Windows 10)上运行。 当我尝试使用托管VMware的同一台计算机(但不是来自VMware本身)连接到服务器时,连接正常,我得到了响应。 但是当我尝试连接同一网络上的另一台计算机(相同的WIFI连接)时,它会失败并且我收到一个没有端点监听的错误。 我必须从2台不同的计算机上建立连接,这是我的第一个学位的项目,所以任何帮助都会非常感激。
修改 因此,我没有从VMware运行服务器,而是在没有Vmware的计算机上运行服务器。然后我从ipconfig获取ip,显然它是一个动态ip,但当我尝试从另一台计算机连接到它时,它的工作原理。 那么我在这里做错了什么? 我可以在VMware和托管的计算机之间建立连接。我可以在2台计算机之间建立连接,但我似乎无法在VMware和托管它的计算机之间建立连接。