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");
Related
I have a Windows Service that hosts a WCF service and I am successfully able to connect to it using WCFTestClient and a Custom Client. The windows service is based upon what used to be an exe, but since the program will be used for a long running process on a server, the service is a better route. The problem is that I cannot access static variables in the application from the WCF service.
In the .exe (I switched this to a .dll which is the server application) I use a global class implemented as such:
public static class Globals
{
....
}
This holds references to the major parts of the program so that if any part needs to reference another I can use the syntax Globals.JobManager.RunJob().
The problem that I am encountering is that the WCF service is not able to reference Globals at run-time. One example of where I need this to be done is in the GetJob method:
public class ConsoleConnection : IConsoleConnection
{
public string[] RetrieveJobList()
{
string[] jobs = Globals.JobManager.GetAllJobNames().ToArray();
return jobs;
}
}
This method returns null when tested in WCFTestClient and throws an exception in the created client.
I believe this issue to be caused by the way the Windows Service, WCF Service, and the application DLL are initiated. The current method is such:
public class ETLWindowsService : ServiceBase
{
....
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
Globals.InitializeGlobals();
serviceHost = new ServiceHost(typeof(ConsoleConnection));
serviceHost.Open();
}
....
}
Here the Windows Service starts, Calls the Globals.InitializeGlobals() that creates all the necessary parts of the application, then starts the WCF service (If this is the wrong way to do this, let me know. I'm piecing this together as I go). I'm assuming that these actions are being done in the wrong order and that is the cause of the problems.
Do I need to have the Windows Service create the WCF Service which in turn creates the application (this doesnt make sense to me), or do I have the Windows Service create the application which then creates the WCF Service? Or is there a third option that I am missing?
The application is in a .dll with the WCF in a separate .dll
I totally agree with Andy H.
If I review this kind of code, I won't try to make the stuff work with the global static variable (even if in the end this is probably possible). A static global class is smelly. First of all, I will figure out to make it work without it.
There are several solution: dependency injection, messaging communication, event driven...
To help you: a long running process in a web service is very common, youy have a good description
here. But in any case, it never uses a static class to synchronize the jobs :)
Improve your design, and you will see that your current problem won't exist at all.
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.
I were studying WCF here http://msdn.microsoft.com/ru-ru/library/bb386386.aspx and I successfully did Testing the Service step. However in Accessing the Service step I faced problems. It builds without any error, but when I tried to write smth to textLabel space and pressed button1 I get the error in button1_Click function, namely
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
Error message
Could not find default endpoint element that references contract >'ServiceReference1.IService1' in the service model client configuaration section. This might be because no configuaration file >>was found for your application or because no end point element matching this contract could >>be found in the client element.
I find such code in the app.project file
<endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService11"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService11" />
I`m 100% sure, that code is without any error, because I copied it from the above site without any modification. So I will be glad to hear your assumptions how fix that.
You should specify the name of the endpoint when constructing the client:
using (var client = new ServiceReference1.Service1Client("BasicHttpBinding_IService11"))
{
client.SomeMethod();
}
or use * if you have only one endpoint in the config file:
using (var client = new ServiceReference1.Service1Client("*"))
{
client.SomeMethod();
}
The reason you need to specify the name is because you could have multiple endpoints (with different bindings for example) for the same service in the config file and if you do not specify the name the framework wouldn't know which endpoint you want to invoke.
Also notice how I have wrapped the IDisposable client in a using statement to ensure proper disposal once you have finished working with it.
I have a solution file ( i.e DLL file). i want to create webservice/WCF Service which exposes the methods of DLL file. so that other team can use webservice instead of DLL reference
simply we cannot Add DLL as a reference to another project because another project is using in java..
so i have been provided DLL file and asked me to create one webservice( WCF is also fine) by using DLL file related methods.
please help me and my question is how can i expose DLL methods in Newly created webservice?
webservice/wcf any thing should be fine .
You can try creating a WCF Service, which has reference to this DLL file, You can call the functions in DLL from Operation contract() in your service contract.
And these operation contracts can be called from your other java project.
Of course you can!
If you can't edit the DLL:
Just create a normal webservice solution, and create the web methods that you want to expose in the DLL.
Then just call the appropriate DLL method in each web method.
If you can edit the DLL, just turn the project into a webservice project and expose the appropriate methods
This Beginners tutorial is excellent and should get you pointed in the right direction.
You can expose all dlls from the WebService Application. Add reference to WebService project
[WebMethod]
public bool CheckLogin(string username, string password)
{
bool status = false;
SqlCommand Command = new SqlCommand();
try
{
Command.CommandText="Select count(*) from CM_Users where username='"+username+"' and passwd='"+password+"'";
Command.Connection=DbConnection.OpenDbConnection();
// this is Assembly Loaded from Application
int count=(int)Command.ExecuteScalar();
if(count>0)
status=true;
else
status=false;
DbConnection.CloseDbConnection(Command.Connection);
}
catch (SqlException expmsg)
{
DbConnection.CloseDbConnection(Command.Connection);
}
return status;
}
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.