Changing the url in WSDL generated interface - c#

To start off, i have never coded a SOAP web request before in my life so this has been a learning curve of note.
I am busy writing a service that will make a soap request to a SageX3 web interface in C#. Net (obviously using Visual Studio). We already have similar request on another service that is supposedly working and I am using that as a reference. So took the (supposedly) same wsdl file and created a connected service reference that generated the Reference.cs file which should be the interface I must use to this soap request. But this request url uses a default url that I do not seem to be able to change. I have looked through the class definition and the properties in the interface like "endpoint" and such all seem to be read only. So I looked in the other project how that Refenrence.cs file looks which is completely different. The first part of my generated reference.cs looks like this:
Newly created reference.cs header:
If you look at the original reference.cs file, it is vastly different:
Original created reference.cs header:
What I notice is that the original has public class that also inherits from SoapHttpClientProtocol that will allow the user of that class to setup the url as reqiured. I do not have that option as my generated code is straight up an interface.
This brings me to my question, could it be that I have an invalid wsdl file or working from a different file as the original code?
Was the code generated differently as there might be a different tool used in Visual Studio?
Did the original author make changes to the reference.cs after it was generated? I think this is a possible answer that I need to add a class definition in the reference.cs file.
Or am I on the completely wrong track here.

Related

How to connect to OData service with generated OData client?

I generated the client code from a for OData V4 service (Dynamics 365 metadata) using the OData V4 Client Code Generator project template (https://marketplace.visualstudio.com/items?itemName=bingl.ODatav4ClientCodeGenerator).
The code was generated fine and it compiles. Now the problem is that I simply cannot find a way to make a connection (or context) that can be used when querying data (OData linq queries for example).
All the examples I've read basically just "start right away" with having a Context or a Resource available, but none of them actually specify why or how it's there.
For example https://blogs.msdn.microsoft.com/odatateam/2014/03/11/tutorial-sample-how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class/ shows how to generate the classes and then creates an instance of NorthwindEntities (or XxxxEntities) which, again, I don't have generated (I don't have anything named "Entities" in the generated code).
https://stoneridgesoftware.com/working-with-the-odata-endpoint-in-dynamics-365-for-operations/ just simply creates a new instance of Resources class, which I don't have (what is it?). I have nothing named Resources in the generated code.
Obviously it can't be this difficult so maybe I'm missing something.
EDIT: the generated class contains only classes Crmbaseentity, CrmbaseentitySingle, ExtensionMethods and then the entity classes themselves. There simply are no other classes.
Any ideas how to do this differently or any blog posts that I've missed?
The problem was that I used the code generation template from https://github.com/Microsoft/Dynamics-AX-Integration/tree/master/ServiceSamples/ODataUtility. When I added the OData service to the project as a "service refernce" and used the wizard, then the code was generated normally.
I still don't know the actual reason why the T4 template didn't work.

AutoRest from Swagger but reusing my existing assembly classes

I have a Windows UWP client application that needs to call a REST API hosted by my ASP.NET service. To generate my client proxy I use the following Visual Studio option...
Right click project -> Add -> REST API Client...
I provide the URL of the swagger endpoint and it generates the expected client code. But the downside is it generates all the classes even though in my case I have a shared class library that has all the server side classes defined. This is a pain because the generated classes do not respect the inheritance of my class hierarchy and flattens everything into non-inherited classes.
Is it possible to get AutoRest to reuse an existing .NET library for classes instead of always generating new classes? This was an option when I used the WCF client proxy generator.
It seems like Add REST API client doesn't have advanced setting for reusing. But Add REST API client has two ways for loading metadata file, swagger URL and existing metadata file. By testing on my site, it should be able to update an existing metadata file and to remove or adjust the nodes that you don't want be generated. And then load the updated existing metadata when adding REST API client.
The classes generated may be determined by the metadata json file and the host value. You may also try to submit a request here to see if swagger team can keep the hierarchy when generating the meta file. Or you may need to manual create the proxy to reuse the libraries.
I think it would be fair to describe the "REST API Client" generation tool in Visual Studio as "spartan".
This answer may be too late to help you, or there may be reasons why you can't use a different tool, but in the hope of benefiting you and/or future readers I'll detail how I achieved generating a REST client in NSwagStudio which reuses my existing classes and enums. (NSwagStudio is free and open source, and I have no affiliation).
On the left hand pane, we select our input. Besides the expected Swagger feeds there are some interesting options such as "Web API via reflection" which "uses .NET reflection to analyze ASP.NET Web API or ASP.NET Core controllers" - this is the option I used but my screenshot shows the default Swagger input.
On the right hand pane, click "CSharp Client" and switch to the "CSharp Client" tab.
The magic bullet is to untick "Generate DTO types":
This will cause it to generate only the client, so you can reuse your existing DTOs.
You'll want to specify a namespace for the client, and optionally one or more namespaces which will be added to the generated C# file as using directives. For example, if you want your client to be in the namespace MyNamespace and your model classes are in SomeOtherNamespace you'd enter the following:
It's well worth having a play with the options. A few quick notes about some of the defaults and why I'm happy with them:
The HttpClient is injected and you control the lifecycle (which seems to me a good thing)
A BaseUrl property is defined. I haven't tested this yet but I'm hopeful from looking at the generated code that this will allow me to safely spin up multiple instances of the client class to talk to multiple servers which share the same API
The JsonSerializerSettings property is protected, but can be configured via the UpdateJsonSerializerSettings partial method
I've saved my settings from the File menu and added the .nswag file to source control, so that I can easily regenerate the client in future if necessary.

web reference : How to change proxy class code

i'm using web reference to consume a web service the problem is with a decimal attribute which is optional , its value is not passed the web service. i tried to add default attribute and give it a default value it works fine , but i can not change my wsdl because i have differenet clients working with it (php,java ... clients)
The problem is well described here : msdn post
stackoverflow post
So i want to find a way to change the code of the proxy class generated after the creation of yhe web reference , because i'm sure it's a bug caused by this class which treats optional decimal attributes wrongly .
Solution 1:
Go to your project folder -> Web References -> MyService.
In there, there should be a file called Reference.cs ... that's the file that is created. It contains proxy classes for the webservice.
Also, if you don't want to mistakenly update the reference and delete your changes to the file, update the reference in the csproj file. On the bottom of the project file, you'll find url's to all services you're using. Point it to the WSDL in the same folder.
Solution 2:
Add the service reference to your project
Change your project file and update the url of the service, point it to the wsdl in the same folder as in solution 1
Make the necessary changes to the wsdl in the same folder
Update the reference in VS ... this will create a new Reference.cs ... it will update the proxy classes
NOTE: Depending on the changes made to the WSDL, it might be the web service on the other side might not be able to 'read' your messages. Making a property obligated instead of optional should not break it though.

Project structure for Schema First Service Development using WCF

I have WSDL and XSD as starting point. (WSDL is generated from XSD using WCSF Blue tool). From the WSDL, using a tool, the service code is generated. The project name is “Autogenerated_Service_Project”. Inside this project it will have [ServiceContract] and [DataContract] classes. It has a data contract named “EmployeeDataContract”. In the GetEmployee() service operation, this datacontract is returned to the client.
I have a business layer project named “Business_Project”. It has a method that returns “Employee” entity object.
At present, I am referring the “Business_Project” inside “Autogenerated_Service_Project”.
Business_Project.MyClass b = new Business_Project.MyClass();
EmployeeDataContract d = b.GetAssociate();
return EmployeeDataContract;
The challenge comes when there happens a change in WSDl. When the WSDL is the changed the “Autogenerated_Service_Project” will be recreated and the code mentioned above will be lost.
What is the solution to overcome this code lose?
Note: The “Autogenerated_Service_Project” is the top most project. Ideally, it cannot be referred by any other projects.
You may change the way calling Business layer(may your solution needs additional layer)
But in simple way, you can generate the proxy once, when changes happen to WSDL
handle the changes manually,Or use the tool only for new services.
If the services on WSDL are finely grained, the solution may be applicable.
This can be resolved by using Partial Classes in a different file. The code given in the question can be moved to this new partial class file. This file will persist even if the auto generated file is re-created.

WCF REST issue: where is service.basic.svc.cs

I just started to use WCF Rest project template to design a REST service, for example Collection REST WCF Collection service.
One thing I noticed that the Service.basic.svc.cs file is no longer under the Service.svc as its partial or dependent class file. I tried to find the definitions for CollectionServiceBase and ICollectionService in Service.svc.cs:
public class Service :
CollectionServiceBase<SampleClass>, ICollectionService<SampleClass>
...
those two classes are actually in my %temp% folder as readonly files. It looks like that they are not for editing. How can I make changes of their attributes such as UriTemplate strings? Not sure if I can bring those files back to the project so that I have control of those files (change definitions or remove some interfaces)? I think there may be reason to this change (compare to the example in some of video shows by endpoint.tv Screencasts).
OK. Finally I got an answer at ASP.Net forum, WCF REST Startkit discussion group saying that the codes in Service.basic.svc.cs are moved to Microsoft.ServiceModel.Web.dll. This makes it hard to customize some class attributes to customize template url or xml node names. I was suggested to get the source codes to make changes if I need, but I don't think it is good idea.

Categories