WCF shared types in WSDL - visual-studio

I have to use several separate webservices from the same provider. Basicly every function has its own service (wsdl). For interop every wsdl has references for shared types (ex: xs:import namespace="http://generic.type.com" />).
Adding Service Referances in VS will prefix the service namespace to these types.
Adding two services will generate two separate but identical class:
var context = new Service1.GenericContext();
var contex2 = new Service2.GenericContext();
How can I map/cast these together? I have 20+ services like this.
Tried NamespaceMappings in Reference.svcmap, but faild. I don't know what TargetNamespace and ClrNamespace to use.
ty!

Instead of adding service references you should use svcutil.exe to generate one service proxy file for the endpoints all together.
All service proxy classes are then together in the same namespace you specify with the command line switch /n.
The svcutil.exe call has many parameters then. So I recommend you store it in a batch file or even more comfortable: Place the command call under Build Events in Visual Studio into "Pre-build event command line".
Here is the svcutil call for my client which puts all proxy classes together in ServiceProxy.cs. Most likely you have to modify the path to svcutil.exe and of course the service URLs:
"%PROGRAMFILES%\Microsoft SDKs\Windows\v7.0A\bin\svcutil.exe" /noLogo /noConfig /out:"$(ProjectDir)ServiceProxy.cs" /t:code /i /l:cs /tcv:Version35 /ser:DataContractSerializer /ct:System.Collections.Generic.List`1 /n:*,Oe.Corporate.CRMFacade.Service.Test http://localhost:3615/Client010/MasterDataService.svc http://localhost:3615/Client010/BusinessPartnerService.svc http://localhost:3615/Client010/MarketingAttrService.svc http://localhost:3615/Client010/ProductTransactionService.svc http://localhost:3615/Client010/ProductDataService.svc http://localhost:3615/Client010/ActivityManagementService.svc http://localhost:3615/Client010/PromotionService.svc
UPDATE:
I forgot to mention that the pre-build event will fail unless you add this to the bottom of your .csproj file right above the closing Project element:
<Target Name="PreBuildEvent" Condition="'$(PreBuildEvent)'!=''" DependsOnTargets="$(PreBuildEventDependsOn)">
<Exec WorkingDirectory="$(OutDir)" Command="$(PreBuildEvent)" ContinueOnError="true" />
</Target>

Related

Adding WSDL service reference to VS fail because generated xsd file contains illegal character for TFS

I have a solution that is on TFS. I'm trying to add a WSDL as service reference to it.
The WSDL's URL looks like this: ".../ws/soap;auth=..."
When adding the reference, VS will generate all the required xsd files and the xsd files name would use the URL as its name. When generating it, TFS will try to automatically add the files, and at this point it will detect that I have a .xsd files with illegal character ";" as its name and then the whole service reference generation just failed and stopped.
I have tried adding the wsdl to a solution that is not bound to TFS and it works just fine.
Any workaround? How do I add the wsdl as service reference to my solution if the generated xsd contains illegal character that TFS won't accept?
You can use svcutil to generate proxy classes,svcutil is a .NET tool that retrieves metadata from a web service on a network location or from a WSDL file, and generates a WCF class containing client proxy methods that access the web service operations.
You can use this tool on the command-line interface of VS:
enter image description here
Executing the above command will generate the a proxy class and configuration file on disk D.Then add these two files to your project:
enter image description here
Here's a link about svcutil:
https://learn.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x
Can you try adding a new class to your project, and copy the contents from the generated proxy class file through svcutil (link below, just in case). This way, I believe, you would be able to bypass the illegal characters in XSD.
You would be able to generate proxy class through Visual Studio Command Prompt using SvcUtil command
HTH!

How to access web.config sections in a Web service called from another application?

I'm working on a Web service with Visual Studio, framework 4.7.1. One of its Web methods needs to call another Web service (provided by another company). It converts the parameters it receives (that are consistent with our main application's business logic) into values the other Web service can handle (according to it's own business logic). To do this, it relies heavily on data stored in the Web.config file.
I tested it directly (start the Web service and call the Web methods with automatically generated pages on a Web browser page) and everything worked fine.
Now, I need to build a test application (also in Visual Studio, framework 4.7.1) to call the same Web methods. On first testing it, I noticed that the Web service was trying to access the test application's config file instead of its own (as described in Can't read Web.config with ConfigurationManager.AppSettings ).
So I created an applicationSettings section in the Web.config and moved all the data from appSettings into it. It worked fine.
Now, however, I notice that the same thing happens with the custom sections. One of them looks like this:
<configSections>
<section name="jobTypeLists" type="AdelSoft_WS_FRA.JobTypesSection" />
</configSections>
<jobTypeLists>
<jobTypes>
<jobType codeCustomerType="A" codeJobType="JobForA" />
<jobType codeCustomerType="B" codeJobType="JobForB" />
</jobTypes>
</jobTypeLists>
I can see how such a structure could fit into its own .settings file, but I have another one that is much more complicated. (Like, the text nodes can have up to four ancestors.) To keep this short-ish, I'm not providing it now, but it can easily be arranged.
ConfigurationManager.GetSection("jobTypeLists") returns null when called from the test application. Same with WebConfigurationManager.GetSection("jobTypeLists").
I've also tried accessing the configuration file with ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile), but I can't seem to find my sections in the Configuration object it returns.
I'm not sure it means anything, but the Configuration object's FilePath property contains "C:\Folder\InnerFolder\WebServiceFolder\web.config.config". Why this second ".config"? I tried passing the same string to ConfigurationManager.OpenExeConfiguration(), without the ".config" extension: it returned null. (As it should, I feel.)
The Configuration object has 10 section groups and 22 sections, which I can't make heads or tails from. Likewise, I can list them.
Actually, there are two ways for a Visual Studio project to reference a Web service: as a regular reference (like you would any other project) or as a Web reference.
I was using the former, and therein lay my mistake.
To reference the Web service, I started it, copied the URL from the browser window that it opened, and pasted it into the "URL" text box in the "Add a Web reference" window from my test application. From there on, it worked fine.
(By the way, I have kept the regular reference as well, because I'm using some constants from the Web service to handle return values.)

Compare what files when update the service reference

Say I updated one WCF service, so in the client side I can view the changes by click view history on the service reference node.
My question is if I wonder what are changed between the changesets, what files should I select?
I think that I can select reference.cs to make a comparison. But is it right? There are many other files such as xsd files.
Try to right-click the service reference and view it in object browser. You could see the generated proxy class.
If you want to compare the difference between the updated service reference and the old one, you could generate client proxy class via SVCUtil.exe tool and then check all generated files.
https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/aa347733(v=vs.90)
such as,
Svcutil.exe http://localhost:1300/mex /Directory:D:

adding multiple wcf service references in the same name space

I have 8 wcf services, would like to have them in the same namespace when I add in the visual studio. I tried Service references -> Add Service Reference but every one creating their names space. Please let me know if you need more information !
thanks !
You can't control the namespace from visual studio. However you can use wsdl.exe to generate the proxy/service reference by hand:
wsdl.exe msdn
Use the /namespace parameter to generate in a specific namespace:
/n[amespace]:namespace Specifies the namespace for the generated
proxy or template. The default namespace is the global namespace. When
using the /parameters option, this value is the element
and contains a string. This element must be in the parameters file.

Error adding a service reference to Visual Studio via WSDL

I have been given a WSDL with all the method requests and responses, and all the objects I'll need to use for creating a few web methods.
I've successfully added the WSDL as a "service reference" and I can see the methods and structures and can instantiate them. It all seems to be there. But, I have a problem: the project won't build after I add the WSDL. Here's the error message I am getting when Visual Studio tries to compile Reference.cs:
"Error 2 The type name 'ServiceReference1' does not exist in the type
'WSPELab.WSPELab' C:\Users\JJ\Documents\Visual Studio
2008\Projects\WSPELab\WSPELab\Service
References\ServiceReference1\Reference.cs 21 111 WSPELabSLN
Is it possible that I am receiving this error message because of a namespace error on my part?
Finally, one additional question: With the WSDL added, can I use the structures it contains directly? Or are they just "listings" for me to implement?
A WSDL is a machine-readable file that describes the methods and types exposed by a web service. Many IDEs, including Eclipse and Visual Studio, can import these and create programming language classes that match the definitions given in the WSDL.
For example, importing a WSDL in a Visual Studio C# project will create a Reference.cs file that contains these definitions. You have to instantiate and call these definitions the same as you do for any classes.
var webServiceReference = new WsdlNamespace.ClassDefinedInWsdl();
WsdlNamespace.ParamClassDefinedInWsdl dataToGet;
WsdlNamespace.ReturnCodeTypeDefinedInWsdl retCode = webServiceReference.MethodDefinedInWsdl("params expected by method", out dataToGet);
if (retCode == WsdlNamespace.ReturnCodeValueMeaningAllIsWell)
{
// use properties of dataToGet
}
Giving any more detail would require showing us the actual WSDL.

Resources