I am trying to access a WCF service hosted in a windows service with an android device. I've gone through the xamarin tutorial for consuming an WCF service hosted in IIS, and through an tutorial for hosting WCF with a Windows service and access it with a console application.
Up to now, I've tried to cross those two solutions, but nothing worked. All I can get out of that is, that a FileNotFoundException
is caught, saying that is misses the ServiceReferences.ClientConfig
in the /
directory. So I've tried to embed this file (which was created by the SLsvcUtil), but nothing worked.
Everytime I call Open
, or AddAsync
or something I get an ReferenceNullException.
So, whats the correct way to connect to an WindowsService hosted WCF service with an mobile device?
Ok, i got it to work properly:
I worked myself through the tutorial on CodeProject again. After that, I created an additional android project and added the poxy class. As I was unable to use the App.Config file, I defined the binding in code. The async methods will fail for some reason, but packing them into a Task.Run and awaiting that just works fine as well:
void InitializeServiceConnection()
{
EndpointAddress ea = new EndpointAddress("http://x.x.x.x:9001/CalcService");
BasicHttpBinding bhttpb = new BasicHttpBinding()
{
Security = new BasicHttpSecurity()
{
Mode = BasicHttpSecurityMode.None,
Transport = new HttpTransportSecurity()
{
ProxyCredentialType = HttpProxyCredentialType.None,
ClientCredentialType = HttpClientCredentialType.None
}
},
BypassProxyOnLocal = true
};
objCalcClient2 = new CalcServiceClient(bhttpb, ea);
}
async void DoStuff()
{
dblResult = await Task.Run<double>(delegate { return objCalcClient2.Add(dblX, dblY); });
WriteLine(string.Format("Calling Add >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
dblResult = await Task.Run<double>(() => { return objCalcClient2.Subtract(dblX, dblY); });
WriteLine(string.Format("Calling Sub >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
dblResult = await Task.Run<double>(() => { return objCalcClient2.Multiply(dblX, dblY); });
WriteLine(string.Format("Calling Mul >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
dblResult = await Task.Run<double>(() => { return objCalcClient2.Divide(dblX, dblY); });
WriteLine(string.Format("Calling Div >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
}
void WriteLine(string Text)
{
tvConsole.Text += System.Environment.NewLine + Text;
tvConsole.Invalidate();
}
I am not sure if I have to populate the Transport property to get an unsecured and unauthenticated connection but it worked. Thats all I want.
But the best thing is, this also works in an Xamarin.Forms project as part of the PCL-Library.
private void Initialize()
{
EndpointAddress ea = new EndpointAddress("http://x.x.x.x:9001/CalcService");
BasicHttpBinding bhttpb = new BasicHttpBinding();
bhttpb.Security.Mode = BasicHttpSecurityMode.None;
objCalcClient2 = new CalcServiceClient(bhttpb, ea);
}
public async void DoStuff()
{
dblResult = await Task.Run<double>(delegate { return objCalcClient2.Add(dblX, dblY); });
WriteLine(string.Format("Calling Add >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
dblResult = await Task.Run<double>(() => { return objCalcClient2.Subtract(dblX, dblY); });
WriteLine(string.Format("Calling Sub >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
dblResult = await Task.Run<double>(() => { return objCalcClient2.Multiply(dblX, dblY); });
WriteLine(string.Format("Calling Mul >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
dblResult = await Task.Run<double>(() => { return objCalcClient2.Divide(dblX, dblY); });
WriteLine(string.Format("Calling Div >> X : {0:F2} Y : {1:F2} Result : {2:F2}", dblX, dblY, dblResult));
}
void WriteLine(string Text)
{
Debug.WriteLine(Text);
}
I thought I had to implement this for each device, but no, WCF can be shared too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.