WCF does not generate the properties - c#

I have a .NET 1.1 ASMX and want to use it in a client WinForms app.
If i go wit the old way and add it as a "WebRefrence" method then I will have access to two of its properties which are "url" and "UseDefaultCredentials" and it works fine.
But if I go with the new WCF way and add it as a ServiceReference I still have access to the methods of that ASMX but those two properties are missing.
what is the reason for that?
so for example in the old way ( adding WebReference) these codes are valid:
TransferService transferService= new TransferService();
transferService.Url = "http://something.asmx";
transferService.Credentials = System.Net.CredentialCache.DefaultCredentials;
string[] machines = transferService.GetMachines();
But in the new way ( adding Service Reference )
using(TransferServiceSoapClient transferServiceSoapClient = new TransferServiceSoapClient("TransferServiceSoap"))
{
transferServiceSoapClient.Url = "someUrl.asmx"; //Cannot resolve URL
transferServiceSoapClient.GetMachines(new GetMachinesRequest());
transferServiceSoapClient.Credentials = .... // //Cannot resolve Credentials
}

Because those are configured in the endpoint in your app/web.config or programatically if you prefer. More on configuring a WCF client here.

Related

Dynamically Change WCF Service URL in ASP.NET MVC

I have one solution in which I have 2 projects with:
ASP.NET MVC Application to consume wcf services.
5 WCF services.
I have added one web service reference in the project 1. Now, I need to use different services based on the user e.g.
User Type 1: Only allow to consume Service 1.
User Type 2: Only allow to consume Service 2.
etc.
I have Service URL's like localhost:6227/Service1.svc, localhost:6227/Service2.svc etc.
I have stored all service URL's in the db and I need to change URL for each user type to consume his allowed service only without adding more end points and only change URL from the backend based on user type. I need relevant link or code to solve this problem.
Edit
In Web Config
I have added just this endpoint in the mvc application and I don't want to use web config to change address in here but I want to change address in the code for each user type while application is running.
<client>
<endpoint address="http://localhost:6227/Service1.svc"
binding="customBinding" bindingConfiguration="CustomBinding_IService1"
contract="Service1.IService1" name="CustomBinding_IService1" />
</client>
if i completely realize your question you need dynamic soap service calling. maybe something like this:
private void CallService()
{
var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;
var myEndpointAddress = new EndpointAddress("your url depend on user type");
var client = new ClientService1(myBinding, myEndpointAddress);
var outpiut = client.someCall();
client.close();
}
Not sure if I understand you correctly but you could use below snippet, if it suits.
//assuming you get string URL. Change type as per need.
string reqdURL = GetServiceURL(typeof(userObject));
private string GetServiceURL(Type userType)
{
if (userType == typeof(UserType1))
{
// currently hardcoded but you can replace your own fetch logic
return "localhost:6227/Service1.svc";
}
if (userType == typeof(UserType2))
{
return "localhost:6227/Service2.svc";
}
//and so on
}
You can modify directly your EndPoint Address doing this:
ClientService1 ws = new ClientService1();
ws.Endpoint.Address = new EndpointAddress("Your new URL svc service");

How to call web services from WSDL?

I have save the wsdl into my local computer, so I have create a new project with visual studio (c#) then I have import this file. So I have the WSDL into my project and I have PianoResidentialService under "Service Refereces" folder.
Now I want to try to call a web service. But I don't know how do this.
I have try this code but now works.
public MainWindow()
{
InitializeComponent();
PianoAssistenzialeResidenzialeService.getPianoAssistenziale ws_PA = new PianoAssistenzialeResidenzialeService.getPianoAssistenziale();
}
If I try to get Inspect ws_PA, all field is null.
You probably need to specify the endpoint:
ws_PA.URL = "http://some.server.com/endpoint/";
Then, you should call a method which is generated for the operation you want to call. (Remember that a webservice can have multiple operations.)
var result = ws_PA.Operation(parameters);
You might want to share the WSDL with us and tell us in more detail what you want to achieve.

How to choose a WCF service to connect to?

I have two separate servers with identical WCF services (let's say, WS1 and WS2) and a C# Mobile CF 2.0 project that need to access both of the services.
Can I do something like this on the C# CF2.0 project?
(...)
if (someCondition == true)
{
WS1 aux = new WS1();
}
else
{
WS2 aux = new WS2();
}
aux.service(parameter1);
(...)
note that I want to have the same variable name, independent of which server I'll access. The problem is: I don't know how to declare it outside the conditional statements and when I just declare it inside the conditional statements they're declared as local variables and I don't know how to make the variable public or global.
Any thoughts or help, please?
Since the WCF Service is exactly the same, just running on different servers, then from your client project simply add a service reference to one of them (WS1 for example). This will generate the client proxy for you. Perhaps give it a generic name too, like "serviceX" (replacing X with something appropriate for your application).
Then, in your client config file, copy the client endpoint it created and add another endpoint with the only difference being the address and the endpoint name. Maybe you want to set the endpoint name property on each endpoint to be "WS1" and "WS2" respectively.
Then, in your code, you should be able to do something like this:
(...)
serviceXClient aux = null;
if (someCondition == true)
{
aux = new serviceXClient("WS1");
}
else
{
aux = new serviceXClient("WS2");
}
aux.service(parameter1);
(...)
If you're using .Net 4.0 or higher you could use dynamic typing.
http://msdn.microsoft.com/en-us/library/dd264736.aspx

Reporting Services Web Service, C# .NET code reuse

I'm building a custom front-end for a collection of reporting services servers. I'm adding the ReportingServices2005 web reference to my project using;
http://server/ReportServer_InstanceName/ReportService2005.asmx?wsdl
At the moment my approach is to add this reference for each server, however I'm then struggling with the code reuse aspect. The reporting services classes are then different namespaces.
I'd like to have a method as below;
public string ListReports(Server1WebService.ReportingService2005 service) {
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.Server1WebService.CatalogItem[] children = service.ListChildren("/", true);
string list = String.Empty;
foreach (Server1WebService.CatalogItem i in children) {
if (!i.Hidden)
list += i.Name + "</br>";
}
return list;
}
To make this method reusable I need to know how to refactor this so that any instance of the ReportingService2005 class can be passed regardless of the namespace. At the moment I have to specify Server1WebService for all references to ReportingService2005 and CatalogItem.
Provided that all of the SSRS instances are the same version, You should be able to set the URL property on the proxy object:
Server1WebService server.url = new uri ("http://server/ReportServer_InstanceName/ReportService2005.asmx?wsdl"));
If you have multiple versions to deal with, you may need to provide some type of factory object that can correctly instantiate the correct version.
Hope this helps

How to make your Service-Reference proxy URL dynamic?

I have a web reference to web-service:
using (var client = new GetTemplateParamSoapClient("GetTemplateParamSoap"))
{
TemplateParamsKeyValue[] responsArray = client.GetTemplatesParamsPerId(
CtId, tempalteIds.ToArray());
foreach (var pair in responsArray)
{
string value = FetchTemplateValue(pair.Key, pair.Value);
TemplateComponentsData.Add(pair.Key, value);
}
}
Tried to change a web-reference url from c# code: as advice here:
1) http://www.codeproject.com/KB/XML/wsdldynamicurl.aspx
2) How to call a web service with a configurable URL
3) http://aspalliance.com/283_Setting_Web_Service_References_Dynamically
But I get symbol is missing when trying to do:
client.Url
In addition I couldn't find a property of "Url_behavior"
It sounds like you've already added the service reference, but here's a walkthrough on adding, updating and removing service references.
Once you've got one of those in your project, you can alter the endpoint URI with one of the constructor overloads, as John Saunders said above. To do this, you'll need to know the name of the endpoint in your config file. For instance, after you add your service you might have elements like this in your config file:
<endpoint address="http://bleh.com/services/servicename.asmx"
binding="basicHttpBinding" bindingConfiguration="ServiceNameSoap"
contract="ServiceReference1.ServiceNameSoap" name="ServiceNameSoap" />
Given that endpoint, you can change the address at runtime by using the following overload:
var proxy = new ServiceReference1.ServiceNameSoapClient("ServiceNameSoap",
"http://new-address.com/services/servicename.asmx");
You can also do it after construction, but that becomes a little bit harder. If you need to do so, see the documentation on the Endpoint property and the associated type ServiceEndpoint.

Categories

Resources