Dynamic config Service Reference C# [duplicate] - c#

I have my first WCF example working. I have the host on a website which have many bindings. Because of this, I have added this to my web.config.
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
This is my default binding http://id.web, which works with the following code.
EchoServiceClient client = new EchoServiceClient();
litResponse.Text = client.SendEcho("Hello World");
client.Close();
I am now trying to set the endpoint address at runtime. Even though it is the same address of the above code.
EchoServiceClient client = new EchoServiceClient();
client.Endpoint.Address = new EndpointAddress("http://id.web/Services/EchoService.svc");
litResponse.Text = client.SendEcho("Hello World");
client.Close();
The error I get is:
The request for security token could not be satisfied because authentication failed.
Please suggest how I may change the endpoint address at runtime?
Additional here is my client config, requested by Ladislav Mrnka
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IEchoService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://id.web/Services/EchoService.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IEchoService" contract="IEchoService"
name="WSHttpBinding_IEchoService">
<identity>
<servicePrincipalName value="host/mikev-ws" />
</identity>
</endpoint>
</client>
</system.serviceModel>

So your endpoint address defined in your first example is incomplete. You must also define endpoint identity as shown in client configuration. In code you can try this:
EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
var address = new EndpointAddress("http://id.web/Services/EchoService.svc", spn);
var client = new EchoServiceClient(address);
litResponse.Text = client.SendEcho("Hello World");
client.Close();
Actual working final version by valamas
EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
Uri uri = new Uri("http://id.web/Services/EchoService.svc");
var address = new EndpointAddress(uri, spn);
var client = new EchoServiceClient("WSHttpBinding_IEchoService", address);
client.SendEcho("Hello World");
client.Close();

This is a simple example of what I used for a recent test.
You need to make sure that your security settings are the same on the server and client.
var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;
var myEndpointAddress = new EndpointAddress("http://servername:8732/TestService/");
client = new ClientTest(myBinding, myEndpointAddress);
client.someCall();

app.config
<client>
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="LisansSoap"
contract="Lisans.LisansSoap"
name="LisansSoap" />
</client>
program
Lisans.LisansSoapClient test = new LisansSoapClient("LisansSoap",
"http://webservis.uzmanevi.com/Lisans/Lisans.asmx");
MessageBox.Show(test.LisansKontrol("","",""));

We store our URLs in a database and load them at runtime.
public class ServiceClientFactory<TChannel> : ClientBase<TChannel> where TChannel : class
{
public TChannel Create(string url)
{
this.Endpoint.Address = new EndpointAddress(new Uri(url));
return this.Channel;
}
}
Implementation
var client = new ServiceClientFactory<yourServiceChannelInterface>().Create(newUrl);

Related

C# NetTcpBinding Client bindings from other config file

I have a client which is a windows application called Windows.exe. I have a C# class library called ServiceFacade.dll and it has a config file called ServiceFacade.dll.config. In ServiceFacade.dll.config, I have client side bindings like below
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:5000/MyService"
binding="netTcpBinding"
contract="IMyService"
name="NetTcpBinding_MyService"/>
</client>
</system.serviceModel>
In ServiceFacade.dll, I have code like below to create proxy
NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_MyService");
ChannelFactory<IMyService> chn = new ChannelFactory<IMyService>(binding);
IMyService service = chn.CreateChannel();
Windows.exe calls ServiceFacade.dll to make service calls.
But below line is looking for NetTcpBinding_MyService in Windows.exe.config instead of ServiceFacade.dll.config
How to make below line to see NetTcpBinding_MyService in ServiceFacade.dll.config but not Windows.exe.config ?
NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_MyService");
You definitely can copy the configuration to the ServiceFacade.dll.config from the Windows.exe.config. I would rather create a service endpoint manually than to copy the configuration when I call the service by using ChannelFactory.
Client-side.
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("https://vabqia969vm:21011/");
NetTcpBinding binding = new NetTcpBinding();
binding.OpenTimeout = new TimeSpan(0, 10, 0);
binding.MaxReceivedMessageSize = 2147483647;
binding.ReaderQuotas.MaxStringContentLength = 2147483647;
ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>(binding, new EndpointAddress(uri));
ITestService service = factory.CreateChannel();
var result1 = service.GetData(34);
Console.WriteLine(result1);
}
}
//The service contract is shared between the client-side and the server-side.
[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetData(int id);
}
Wish it is useful to you.
I did like below.
I added below in ServiceFacade.dll.config
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfiguration"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:5000/MyService"
binding="netTcpBinding"
contract="IMyService"
name="NetTcpBinding_MYService"
bindingConfiguration="netTcpBindingConfiguration" />
</client>
</system.serviceModel>
In ServiceFacade.dll, I have code like below to create proxy
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConfigurationChannelFactory<IMyService> chn =
new ConfigurationChannelFactory<IMyService>(
"NetTcpBinding_MyService",
config,
new EndpointAddress("net.tcp://localhost:5000/MyService"));
IMyService iMyService = chn.CreateChannel();

How can I set the address attribute of a System.ServiceModel.Client.Endpoint object in code?

First off, I'm not a web developer, so go easy on me..
I inherited a project which utilizes a web services client. I'm looking to be able to deploy the same .config file across multiple environments without having to edit it directly. To that end, the only attribute that I'm not sure how to deal with is the address in the <system.serviceModel> section:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="CA_ServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1000000" maxBufferPoolSize="524288" maxReceivedMessageSize="1000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="16384" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
<binding name="CA_ServiceSoap1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://myserver.contoso.com/CA_Service.asmx" binding="basicHttpBinding" bindingConfiguration="CA_ServiceSoap" contract="WebServices.CA_ServiceSoap" name="CA_ServiceSoap"/>
</client>
Is there a way I could enter a dummy value for the "address" attribute in the <endpoint> tag in .config, and then properly define it at runtime via code?
It is possible to to change the address of an endpoint with basicHttpBinding in code when creating the client.
Based on the config you provided I guess your code to create a client could be similar to the following:
//Specify the binding to be used for the client.
BasicHttpBinding binding = new BasicHttpBinding() { Namespace = "WebServices.CA_ServiceSoap" };
//Specify the address to be used for the client.
EndpointAddress address =
new EndpointAddress("https://myserver.contoso.com/CA_Service.asmx");
// Create a client that is configured with this address and binding.
CA_ServiceSoap client = new CA_ServiceSoap(binding, address);
Thanks to #S.Dav for pointing me in the right direction on this. What I ended up doing is including everything in the XML that I posted (which itself is just a serialized object) directly in my code. The only attribute that is variable is the URL:
BasicHttpBinding binding = new BasicHttpBinding()
{
Namespace = "WebServices.CA_ServiceSoap",
CloseTimeout = new TimeSpan(0, 1, 0),
OpenTimeout = new TimeSpan(0,1,0),
ReceiveTimeout = new TimeSpan(0,10,0),
SendTimeout = new TimeSpan(0,1,0),
AllowCookies = false,
BypassProxyOnLocal = false,
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
MaxBufferSize = 1000000,
MaxBufferPoolSize = 524288,
MaxReceivedMessageSize = 1000000,
MessageEncoding = WSMessageEncoding.Text,
TextEncoding = Encoding.UTF8,
TransferMode = TransferMode.Buffered,
UseDefaultWebProxy = true,
ReaderQuotas = new XmlDictionaryReaderQuotas()
{
MaxDepth = 32,
MaxStringContentLength = 16384,
MaxArrayLength = 16384,
MaxBytesPerRead = 4096,
MaxNameTableCharCount = 16384
}
};
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport = new HttpTransportSecurity()
{
ClientCredentialType = HttpClientCredentialType.None,
ProxyCredentialType = HttpProxyCredentialType.None,
Realm = ""
};
binding.Security.Message = new BasicHttpMessageSecurity()
{
ClientCredentialType = BasicHttpMessageCredentialType.UserName,
AlgorithmSuite = SecurityAlgorithmSuite.Default
};
client = new CA_ServiceSoapClient(binding, new EndpointAddress(Config.WebServiceURL));
As a future to-do, I plan to see if any of these binding attributes might need to be adjusted for troubleshooting/performance & add them as appsettings which I will then reference in the BasicHttpBinding instantiation.

Wcf basic auth not accepting username

I am trying to consume a SOAP service and am having authentication problems and think I may be missing something useful. I think the issue may be in that I'm passing in the header more than just the credentials (which I believe is what I have to do, but think it just makes the situation more unique). Below is my config file and the code I'm using to authenticate.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MemberSoap">
<security mode="Transport" />
</binding>
<binding name="MemberSoap1" />
<binding name="TransactionSoap">
<security mode="Transport" />
</binding>
<binding name="TransactionSoap1" />
<binding name="CertificateSoap" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
<readerQuotas maxDepth="32" maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
<security mode="Transport" />
</binding>
<binding name="CertificateSoap1" />
<binding name="MembershipSoap">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="None" realm="" />
<message clientCredentialType="Certificate" algorithmSuite="Default" />
</security>
</binding>
<binding name="ContentSoap">
<security mode="Transport" />
</binding>
<binding name="ContentSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://membership/member.asmx"
binding="basicHttpBinding" bindingConfiguration="MemberSoap"
contract="Member.MembershipWsMemberSoap" name="Ovs.Membership.Ws.MemberSoap" />
<endpoint address="https://membership/transaction.asmx"
binding="basicHttpBinding" bindingConfiguration="TransactionSoap"
contract="MembershipWsTransactionSoap" name="TransactionSoap" />
<endpoint address="https://membership/certificate.asmx"
binding="basicHttpBinding" bindingConfiguration="CertificateSoap"
contract="MembershipWsCertificateSoap" name="CertificateSoap" />
<endpoint address="https://ngmembership/Membership.svc"
binding="basicHttpBinding" bindingConfiguration="MembershipSoap"
contract="Membership.IMembership" name="MembershipSoap" />
<endpoint address="https://membership/content.asmx"
binding="basicHttpBinding" bindingConfiguration="ContentSoap"
contract="Content.MembershipWsContentSoap" name="ContentSoap" />
</client>
</system.serviceModel>
<appSettings>
<add key="UsernameAuth" value="user" />
<add key="PasswordAuth" value="pass" />
</appSettings>
</configuration>
I left out the base url for security purposes as well as the full namespaces, but the one I'm mainly concerned about is the name="MembershipSoap" service. Here is my first attempt at the code I'm using to authenticate.
public Transaction[] GetAllBookingInfo(string memberId, string partnerId)
{
AllBookingsByMemberIdRS response;
using (var client = new MembershipClient())
using (new OperationContextScope(client.InnerChannel))
{
// add the basic auth header
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name]
= GetBasicAuthHeader("user", "pass");
var request = new AllBookingsByMemberIdRQ
{
MemberId = memberId,
PartnerId = partnerId
};
response = AuthenticateServiceUser.membershipSession.GetAllBookingsByMemberId(request);
}
var trans = response.Transactions;
return trans;
}
protected HttpRequestMessageProperty GetBasicAuthHeader(string userName, string password)
{
// get the basic auth header
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + password));
return httpRequestProperty;
}
At this I get an error saying that there were 'Invalid web service credentials used'. So after reading I switched the first method to this.
public Transaction[] GetAllBookingInfo(string memberId, string partnerId)
{
AllBookingsByMemberIdRS response;
var client = new MembershipClient();
client.ClientCredentials.UserName.UserName = "user";
client.ClientCredentials.UserName.Password = "pass";
using (new OperationContextScope(client.InnerChannel))
{
var request = new AllBookingsByMemberIdRQ
{
MemberId = memberId,
PartnerId = partnerId
};
response = AuthenticateServiceUser.membershipSession.GetAllBookingsByMemberId(request);
}
var trans = response.Transactions;
return trans;
}
And now I'm getting a 'The username is not provided. Specify username in ClientCredentials' error. So now I feel like I'm moving further away to where I was before. Maybe someone can shed some light on this? Thanks in advance!
I figured it out. I stupidly reference another client in my code - one without credentials. It's always those simple mistakes that get overlooked.
public Transaction[] GetAllBookingInfo(string memberId, string partnerId)
{
AllBookingsByMemberIdRS response;
using (var client = new MembershipClient())
using (new OperationContextScope(client.InnerChannel))
{
// add the basic auth header
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name]
= GetBasicAuthHeader("user", "pass");
var request = new AllBookingsByMemberIdRQ
{
MemberId = memberId,
PartnerId = partnerId
};
response = client.GetAllBookingsByMemberId(request);
}
return response.Transactions;
}

WCF Authentication error connecting to IP Address

I have a program which have a wcf service to communicate with other module. I'd like to implement custom authorization and authentication. Sorry for bad code. Here is it:
Server:
Config:
<behaviors>
<serviceBehaviors>
<behavior name="managementMexBehavior">
<serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:7538/management/mex"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
<serviceDiscovery>
<announcementEndpoints>
<endpoint kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>
</serviceBehaviors>
</behaviors>
<binding name="managementServerBindingConfig" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
transferMode="Buffered" maxReceivedMessageSize="65535">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
Code
var binding = new NetTcpBinding("managementServerBindingConfig");
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
string address = _c24ServerAdminSettings.ManagementWebServerAddress;
ServiceEndpoint endpoint = Host.AddServiceEndpoint(ServiceInterface, binding, address);
endpoint.Name = "C24ServerAdminManagementEndpoint";
var parametrInspector = new OperationParametrInspector();
var errorHandler = new DispatcherErrorHandler();
errorHandler.OnHandleError += errorHandler_OnHandleError;
var behavior = new EnpointDispathcherBehavior(parametrInspector, errorHandler);
endpoint.Behaviors.Add(behavior);
//ServiceCredentials
ServiceCredentials scb = Host.Description.Behaviors.Find<ServiceCredentials>();
if (scb == null)
{
scb = new ServiceCredentials();
Host.Description.Behaviors.Add(scb);
}
scb.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
scb.UserNameAuthentication.CustomUserNamePasswordValidator = new PasswordValidator(_dataManager);
scb.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "localhost");
//ServiceAuthorizationBehavior
ServiceAuthorizationBehavior sab = Host.Description.Behaviors.Find<ServiceAuthorizationBehavior>();
if (sab == null)
{
sab = new ServiceAuthorizationBehavior();
Host.Description.Behaviors.Add(sab);
}
sab.PrincipalPermissionMode = PrincipalPermissionMode.Custom;
sab.ExternalAuthorizationPolicies = new ReadOnlyCollection<IAuthorizationPolicy>(new[]
{
new AuthorizationPolicy()
});
Client:
Config:
<binding name="C24ServerAdminManagementEndpoint" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
<endpoint address="net.tcp://localhost:60001/Management/" binding="netTcpBinding"
bindingConfiguration="C24ServerAdminManagementEndpoint" contract="C24ServerAdminManagement.IManagementWebService"
name="C24ServerAdminManagementEndpoint">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
Code:
ManagementWebServiceClient ds = new ManagementWebServiceClient("C24ServerAdminManagementEndpoint", _managementServiceAddress);
ds.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
ds.ClientCredentials.UserName.UserName = UserName;
ds.ClientCredentials.UserName.Password = Password;
ds.Open();
This work pretty well with localhost. But when I set computer Ip address. Client trying to connect to service, service respond and exception occurs.In exception said that response received from DNS(localhost) while we wait from DNS(192.168.0.1). But 192.168.0.1 is local address.
I was having the same problem "...everything OK if the client and host are on the same machine, but if the Host and Client are on separate machines I get exceptions errors".
This is what solved the problem for me: My internet connection settings used a proxy server. I changed the IE options for the LAN settings to Bypass proxy server for local addresses and Do not use proxy server for addresses beginning with: http:\\host-ip-here
Good luck.
The problem was in dns identity. I used localhost certificate. And when i connected using direct IP service returned DNS from certificate.Actually adding dns identity in config should have fixed that problem. Maybe it didn't fix because i created endpoint in code and it load binding config but not endpoint. I rewrite code just a little
string address = _managementServiceAddress;
EndpointAddress epa = new EndpointAddress(new Uri(address), EndpointIdentity.CreateDnsIdentity("localhost"));
ManagementWebServiceClient ds = new ManagementWebServiceClient("C24ServerAdminManagementEndpoint", epa);
ds.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
ds.ClientCredentials.UserName.UserName = UserName;
ds.ClientCredentials.UserName.Password = Password;
It works fine.

WCF Services issue? (2 way connection)

I have simple chat program using WCF service. One service use for server and another use for client. Those services connect to each other and call each other. For hosting server, I used a windows service and for client I host WCF service in a Windows app. After all I found that this code work on simple computer, but when move server service to another computer an exception raised and server can't connect to the client. I searched and try other ways.
I get a result:
*IF WCF SERVICE HOST IN WINDOWS APP U CAN'T CONNECT TO IT FORM ANOTHER COMPUTER.
*THIS CODE WORKED ONLY WHEN I USED TWO WINDOWS SERVICES (hosting WCF client service in a windows service)
But I want to know HOW hosting WCF service in windows app that can connect and work with another services?
This is my code
Client code:
Manager.cs
public delegate void UserInfoHandeler(string UserName);
public delegate void MessageHandeler(string Message);
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Manager : IClientPoint
{
public void SendUserList(string[] users)
{
frmRoom.Members = users; // this method called by Server (WCF service which host in windows service)
//when server call this method I have an exception with SSPI
}
public void SendMessage(string message)
{
frmRoom.ReciveMessage = message; // this method called by Server (WCF service which host in windows service)
//when server call this method I have an exception with SSPI
}
FrmJoin frmJoin;
FrmRoom frmRoom;
ChatServerClient ServiceInvoker;
public string User
{
get;
set;
}
public void Run()
{
frmJoin = new FrmJoin();
frmJoin.LoginEvent += new UserInfoHandeler(frmJoin_LoginEvent);
ServiceInvoker = new ChatServerClient("WSHttpBinding_ChatServer", Settings.Default.ChatServerAddress);
frmJoin.ShowDialog();
}
void frmJoin_LoginEvent(string UserName)
{
frmRoom = new FrmRoom();
frmRoom.SendMessageEvent += new MessageHandeler(frmRoom_SendMessageEvent);
frmJoin.LogoutEvent += new UserInfoHandeler(frmJoin_LogoutEvent);
User = UserName;
frmRoom.ReciveMessage = ServiceInvoker.Login(User, Settings.Default.ClientPointAddress);
frmRoom.ShowDialog();
}
void frmJoin_LogoutEvent(string UserName)
{
string message = ServiceInvoker.Logout(UserName, Settings.Default.ChatServerAddress);
}
void frmRoom_SendMessageEvent(string Message)
{
ServiceInvoker.SendMessage(User, Message);
} }
Client config:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_Config" closeTimeout="00:05:00"
openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
<binding name="MyConfig" closeTimeout="00:10:00" openTimeout="00:10:00"
sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_Config"
contract="Host.IChatServer" name="WSHttpBinding_ChatServer">
</endpoint>
</client>
<behaviors>
<serviceBehaviors>
<behavior name="Room.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="Room.Service1Behavior" name="Room.Manager">
<endpoint address="" binding="wsHttpBinding" contract="Room.IClientPoint" bindingConfiguration="WSHttpBinding_Config">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
http://PChost:8731/ClientPoint/
http://PCserver:8731/ChatServer/
Server code:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ChatServer : IChatServer
{
Dictionary clients;
public ChatServer()
{
clients = new Dictionary<string, ClientInvoker>();
}
public string Login(string Username, string address)
{
try
{
ClientInvoker client = new ClientInvoker("WSHttpBinding_ClientPoint", address);
clients.Add(Username, client);
foreach (ClientInvoker clientinvoker in clients.Values)
clientinvoker.SendUserList(clients.Keys.ToArray());
}
catch (Exception e)
{
File.AppendAllText(#"c:\ServiceChatLog.txt", "Service trow Exeption \n");
File.AppendAllText(#"c:\ServiceChatLog.txt", e.ToString() + " \n");
}
return string.Format("Welcom {0}", Username);
}
public string[] GetListUser()
{
return clients.Keys.ToArray();
}
public void SendMessage(string userName, string ReciveMessage)
{
string message = string.Format("{0} : {1}", userName, ReciveMessage);
foreach (ClientInvoker clientinvoker in clients.Values)
clientinvoker.SendMessage(message);
}
public string Logout(string Username, string address)
{
clients.Remove(Username);
foreach (ClientInvoker clientinvoker in clients.Values)
{
clientinvoker.SendUserList(clients.Keys.ToArray());
clientinvoker.SendMessage(string.Format("{0} left ROOM", Username));
}
return string.Format("Godbye {0}", Username);
}
}
Server config:
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_Config"
contract="Room.IClientPoint" name="WSHttpBinding_ClientPoint">
</endpoint>
</client>
If you need to use 2-way communication, maybe you should take a look at WCF Duplex Services.
*IF WCF SERVICE HOST IN WINDOWS APP U CAN'T CONNECT TO IT FORM ANOTHER COMPUTER
This couldn't be further from the truth. You can check a few things:
The server's firewall -- you're using a non-standard port, 8731, are you sure it's allowed?
The address -- can you connect to that IP and Port from the client normally? Try using telnet or putty, or expose the WSDL on the server and hit it through a browser.
Security -- the client endpoint is using Windows authentication -- are the two machines on the same domain or is the same user configured on both servers?

Categories