I have added a Service Reference to my Windows Phone 8 Project and it is showing in the Service Reference folder.
When I try to create an instance of the service, such as:
var service = new MobileService();
It doesn't recognise what MobileService is. Same goes for when I try doing using MobileService; at the top of the class.
Where am I going horribly wrong?
The class is probably called MobileServiceClient. Click the show all files option for your project and you can view the generated code of the service references.
Related
I've created the WCF service and some simple WPF application consuming it. When I'm running the project from within Visual Studio, the WCF Test Client opens and the application works just fine, method defined in service work.
But I need to host this WCF service in a Windows Service. I've followed this, installed the services using Installutil.exe and the ran the service. Everything went fine, it's working.
Yet, when I'm trying to open the executable file with WPF application directly from the debug folder of the app, I'm getting this error:
zad8. has stopped working
After choosing the option to debug it with new instance of VS I get
XamlParseException occured in PresentationFramework.dll
The stack trace shows something like:
connection can't be started, because the target computer is actively refusing it
Do you have any idea what could go wrong?
Fortunately, I've managed to come up with solution. I think I should post it, maybe one day it will help somebody:)
I actually did two mistakes, but one of them was unfortunately caused by the mentioned tutorial (here) in connection with my temporary blackout.
In step 5, point 8 of this tutorial, there's an example of overriding OnStart() method:
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(Service1));
myServiceHost.Open();
}
Beware, that Service1 is ambiguous in this context, because it's name of the Windows Service project class as well as the name of WCF Service class. It should be written with fully qualified name (here it is WcfServiceLibrary1.Service1). In my case, the service name was different, and I just put the Service1 in there in a hurry. Anyway..
In case, someone has it all behind and still encounters the same problem (with app stopped working), I think that you should try open the project in Visual Studio and try to debug the client consuming application as a new instance (right click on the project-> Debug -> Start as new instance...).
It might seem trivial, but when u hit F5 or Ctrl+F5 then even if u have only those project set as startup project, VS will host it's client anyway. In my case it did matter, because I needed to use isolation storage file. And as it was kept on the service side, then I had this file created in IIS server created by VS. Somehow, my method of creating such file had set FileMode.Open() and it was causing the crush, because in Windows Service it didn't exist and the new one couldn't be created and that was neccessary to run it correctly.
What's more it just showed me that this question couldn't be answered properly, cause the data I've provided was not enough and it was delicate.
Cheers:)
I have the following problem,
I wish to install an application of mine as a service on the system.
My class inherits from System.Configuration.Install.Installer. The problem starts when in the constructor of that class I try to get the parameters of Context property, then I get the following exception:
Unable to create an instance of (my class name that inherits from installer) installer type.
When I printed out the Context property, I saw it was null (and it throws the exception when trying to reach the context property) in the constructor.
Same code works fine on 2003 and installation is finished successfuly, but here it fails.
Move your code from constructor to Install, if possible. A common usage of Installer would be:
using ( TransactedInstaller transactedInstaller = new TransactedInstaller() )
{
transactedInstaller.Installers.Add(myInstaller);
transactedInstaller.Context = new InstallContext(null, null);
transactedInstaller.Install(new System.Collections.Hashtable());
}
That's why Context is null in the constructor.
So I created winforms client and added wcf class library to the solution.
In winforms I do
ServiceHost svc = new ServiceHost(typeof(...), new Uri("net.pipe://localhost/MyNamedPipe")
and then svc.Open() which executes fine.
Now, how do I add a service reference so in same winforms I can get proxy for that wcf?
I only was able to generate that by using ASP.NET Development Server which started when winforms was ran and so I copied that url, stopped debugging (Development Server was still running) and then added a service reference from there. But that isn't correct I guess.
Of course I can reference wcf contract class directly and use it, but that is not proper either.
When you are controlling both ends like that, I prefer to use ChannelFactory:
NetNamedPipeBinding binding = new NetNamedPipeBinding();
EndpointAddress address = new EndpointAddress("net.pipe://localhost/MyNamedPipe");
ChannelFactory<YourInterface> factory = new ChannelFactory<YourInterface>(binding, address);
YourInterface yourInterface = factory.CreateChannel();
Have you tried adding a Service Reference... to the project, then entering your URI directly in the Address box of the dialog?
Note that this should be the complete URI, such as net.pipe://localhost/MyNamedPipe.
You can find step-by-step instructions from MSDN here.
I have created one WCF service , which is working fine, now i want to consume it in a client application.
using SVCutil.exe i have generated proxy and aap.settings for that service and added that to the client sln(console application)
But the problem is i am unable to access the wcf methods.
using System.ServiceModel;
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p. // not getting the wcf methods
}
}
}
what I am doing wrong?
Depends on how your service is called. When you created the service reference, you gave it a namespace name - in that namespace, there should be a class called (yourservicename)Client - instantiante one of those and get going.
You should find those files under the Service Reference - if you click on the "show all files" button in the Solution Explorer, you'll start seeing a ton of files under your service reference - one in particular should be Reference.cs. Those classes are defined in that file - you can check it out, it's a regular C# file.
Update: If you create your proxy using svcutil.exe, depending on your options used with svcutil, you should also get a .cs file that contains the classes needed.
svcutil http://yourserver/yourservice
would create a file called (your WSDL name).cs and an output.config in that directory where you run this command.
You can also specify a file name for the C# file:
svcutil http://yourserver/yourservice /out:MyService.cs
and then your file is called MyService.cs.
SvcUtil has a ton of options - can't explain them all to you, play around with them, read up on the MSDN docs for it.
Again, one of them will be called (your service name)Client. Include that *.cs file in your project, check the namespace, create an instance of the .....Client class and use it to call the WCF service.
Example:
Grab info from URL
svcutil http://www.ecubicle.net/iptocountry.asmx?wsdl /out:IP2CountryClient.cs
Include the resulting IP2CountryClient.cs in your project; by default, the classes in that file are in no particular namespace, so they're globally visible
Instantiate the client class iptocountrySoapClient
iptocountrySoapClient client = new iptocountrySoapClient();
Call methods - e.g. this one here:
string result = client.FindCountryAsString("82.82.82.82");
I'm connecting to a web service hosted by a third-party provider. I've added a service reference in my project to the web service, VS has generated all the references and classes needed.
I'm connecting with this piece of code (client name and methods anonymized):
using (var client = new Client())
{
try
{
client.Open();
var response = client.Method(...);
return response.Status;
}
catch (SoapException ex)
{
throw CreateServiceException(ex);
}
finally
{
client.Close();
}
}
When reaching the client.Open(), I get an exception with this message:
The top XML element '_return' from
namespace '' references distinct types
System.Boolean and
Service.Status.
Use XML attributes to specify another
XML name or namespace for the element
or types.
In reference.cs, I can see that the "_return" variable is decorated with
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
Is there a problem with the wsdl, the generated service reference or in my code?
Update: Generating the service as an old school Web Service solves the problem. I've marked Sixto's answer as accepted for now, but I'm still curious what could've caused the problem and if any parameters to the service generator could solve the original problem.
If you were able to create a service reference then the WSDL is valid. The exception message is saying you have namespace/type ambiguity problem with _return. The generated code is probably using it in some context as a boolean and in another context as a Service.Status type.
I don’t call the ClientBase.Open method before invoking a service method because I’ve never seen the need for it. I do always call the Close & Abort methods as appropriate. The Open method basically just changes the state of the client to no longer be configurable. I’m not sure how that would trigger code in the generated class since it is an inherited method. I’d try just removing that line and see if you get the same exception. Otherwise, if you haven’t already done so, search the generated code for all the places _return is used and see if you can manually sort-out the appropriate type. You may need different names for each context.
Another way to troubleshoot the WSDL is to create a Web Reference (assuming it’s an HTTP based service) and see if the generate code works as expected. If it does work, go with the ASMX client unless you have a need for WCF proxy capabilities.