Chcę uzyskać połączenie z usługą uruchomioną na innym komputerze. Używam NetTCPBinding do komunikacji.
Kiedy usunę SecurityMode z pliku konfiguracji otrzymuję wyjątek: Serwer odrzucił poświadczenia klienta. Ten wyjątek występuje tylko na innym komputerze, na komputerze na której uruchomiłem usługę klient działa poprawnie.
Kiedy SecurityMode=None zarówno na komputerze na którym zainstalowana jest usługa jak i na innym PC otrzymuję w kliencie wyjątek:
System.ServiceModel.CommunicationException: „Połączenie gniazda zostało przerwane. Mogło to być spowodowane błędnym przetwarzaniem komunikatu, przekroczeniem limitu czasu odbioru przez zdalny host lub problemem z zasobami sieciowymi podległej sieci. Limit czasu lokalnego gniazda wynosi „00:00:59.9910009”.”
Inner Exception:
SocketException: Istniejące połączenie zostało gwałtownie zamknięte przez zdalnego hosta
Próbuję już ponad tydzień przez to przejść. Proszę o pomoc bo straciłem nadzieję
Kontrakty:
[ServiceContract(CallbackContract =typeof(IClient))]
public interface IServer
{
[OperationContract(IsOneWay =true)]
void Connect(string MAC);
[OperationContract(IsOneWay = true)]
void Disconnect(string MAC);
}
public enum eClientConnectionError
{
MACRegistered,
}
public enum eClientDisconnectedResult
{
Success,
ClientNotFound
}
[ServiceContract]
public interface IClient
{
[OperationContract(IsOneWay = true)]
void ClientConnected(string Message);
[OperationContract(IsOneWay = true)]
void ClientDisconnected(eClientDisconnectedResult Result);
[OperationContract(IsOneWay = true)]
void ClientConnectionError(eClientConnectionError ErrorID);
}
Tutaj kod serwera:
public partial class MainWindow : Window
{
ServiceHost host = null;
public MainWindow()
{
InitializeComponent();
host = new ServiceHost(typeof(GsService));
host.Open();
}
}
Tutaj plik konfiguracyjny serwera:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<services>
<service name="ClientServerInterface.GsService" behaviorConfiguration="MyServiceBehavior">
<endpoint name="NetTCPBinding_GsService"
address="net.tcp://192.168.1.163:2222/GsService.svc"
binding="netTcpBinding"
contract="ClientServerInterface.IServer" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://192.168.1.163:2222/GsService" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="NetTCPBinding_GsService">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="65536" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Kod klienta:
public partial class MainWindow : Window, GSServer.IServerCallback
{
public MainWindow()
{
InitializeComponent();
Context = new InstanceContext(this);
Context.Opening += Context_Opening;
Context.Opened += Context_Opened;
Context.Faulted += Context_Faulted;
Server = new GSServer.ServerClient(Context);
}
private void Context_Faulted(object sender, EventArgs e)
{
Dispatcher?.Invoke(() =>
{
hList.Items.Add("Context_Faulted");
});
}
private void Context_Opened(object sender, EventArgs e)
{
Dispatcher?.Invoke(() =>
{
hList.Items.Add("Context_Opened");
});
}
private void Context_Opening(object sender, EventArgs e)
{
Dispatcher?.Invoke(() =>
{
hList.Items.Add("Context_Opening");
});
}
MyCallback Callback = new MyCallback();
InstanceContext Context = null;
GSServer.ServerClient Server = null;
public void ClientConnected(string Message)
{
Dispatcher?.Invoke(() =>
{
hList.Items.Add(Message);
});
}
public void ClientDisconnected(eClientDisconnectedResult Result)
{
Dispatcher?.Invoke(() =>
{
hList.Items.Add("Rozłączono, status: " + Result.ToString());
});
}
public void ClientConnectionError(eClientConnectionError ErrorID)
{
Dispatcher?.Invoke(() =>
{
hList.Items.Add("Błąd połączenia: " + ErrorID.ToString());
});
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Dispatcher?.Invoke(() =>
{
/*
try
{
Server.Open();
Server.Connect("121212");
}
catch(Exception exc)
{
hList.Items.Add(exc.Message);
}
*/
Server.Open();
Server.Connect("121212");
});
}
}
Plik konfiguracyjny klienta:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTCPBinding_GsService">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://192.168.1.163:2222/GsService.svc"
binding="netTcpBinding" bindingConfiguration="NetTCPBinding_GsService"
contract="GSServer.IServer" name="NetTCPBinding_GsService"/>
</client>
</system.serviceModel>
</configuration>