In Visual Studio, how do I call my Web API? - visual-studio

In Visual Studio, I have one project that is an Angular application and a second project that is ASP.NET Web API. I am just starting work and don't want to post anything to production. Is there a way within Visual Studio to setup the Web API project so I can call it? I tried this:
$http.get('http://localhost:45620/Sample.WebAPI/api/unitSizes')
.then(onSizeRetrieveComplete, onError);
And several other variations of this, but I keep getting a 404 (not found).
Any suggestions?

OK I figured this out.
Several things have to happen to make this work:
Set the Solution Set Startup Projects to start up both projects.
Set the Web API project properties Web | Start Action to "Don't
open a page."
Remove the name of the project from the url in the
sample code. So it then looks like this:
$http.get('http://localhost:45620/api/unitSizes')
.then(onSizeRetrieveComplete, onError);
Ensure that, if using a plural name, you use the plural for the
controller name as well (UnitSizesController).
That did it for me. Hope this helps someone else ...

Don't code your javascript directly to your localhost, you will have a hard time publishing to production. Use a relative Url instead,
$http.get('/api/unitSizes')
.then(onSizeRetrieveComplete, onError);

Related

Azure function not showing up in portal

I've written my first Azure Function and I've published it to azure. It said that the publish was successful and it created a new function app, but it's an empty one. I've done a bit of research into why my function wouldn't show up and I've seen that you might need to change the path to the zip deploy but I can't find where I need to change this and into what. Other solutions are always welcome! (I've published it directly from VS2017)
When I send a Postman Request to the url/api/function, it also doesn't work. It doesn't crash or anything, it just gives back nothing after a second.
I need this to work to create a custom connector for Power Automate that way I can use it in my Microsoft Flow.

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.)

Dynamically set URL of web service

Scenario: I'm running code on the client that connects to a server and uses a web service to retrieve data about a SharePoint list. I'm using the Visual Studio 2010 "service reference" to get a web service for my SP Site and get my data from the list. It works. Now how do I go about coding it such that when I want to move from Test to Production, my web service calls will still work? Note that the web service is a SharePoint web service, I did not write it. I am only using it. Is what I am suggesting possible? I do have the ability to ensure that the Site is the exact same (except for URLS) on both environments (e.g. backup the SP site and put it on production). Thanks for any suggestions.
Summary:
Basically I'm looking for the best way to go from test to production without re-compiling my code which consumes the SP web service. Also, as a side note, if anyone knows how similar the test/production sharepoint sites have to be, [in order for the web service to work on both without anything but the URL being changed].. that would be helpful info.
Solution
The project configuration file can be used to specify the web service location. The .svcdatamap and other files in the VS project are for design time use only, and the URL which is actually used to connect to the SharePoint web service is passed as an argument to the System.Data.Services.Client.DataServiceContext object. This is only tangentially related, but to create your own WCF web service see this link. BTW, the web service will work without recompilation anywhere the SharePoint List has the same List name and the column you're querying has the same name.
As far as I know, Visual Studio Tools for Office projects allow you to have an app.config file in your project. I'd expect that Visual Studio created the app.config file and added the necessary configuration settings related to the web service reference. In any case, you need to store the correct web service url somewhere - app.config, registry or even a database.
If you are not able to store the web service endpoint address information in the app.config file, there's a way to configure the proxy manually.
If it's a .asmx reference added as a legacy "Web Reference" in Visual Studio then all you need to do is set the Url property value of the proxy object before you call any web service methods. For example:
MyASMXWebService proxy = new MyASMXWebService();
proxy.Url = "web service url";
proxy.HelloWorld();
If it's a .svc WCF service reference then things get a bit more complicated. You'll need to create your web service endpoint programmatically. For example:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress("web service url");
ChannelFactory<IMyWCFWebService> factory = new ChannelFactory<IMyWCFWebService>(binding, endpoint);
IMyWCFWebService proxy = factory.CreateChannel();
proxy.DoWork();
Since SharePoint services (SVC) do not provide a proxy, option 2 will not work. References to both locations must be included in your application and then use a parm to differentiate production from test. I believe 2013 will likely correct this bug.
You have to edit the URL in your project's web.config
Simple SharePoint example:
MyService.ListsSoapClient client = new MyService.ListsSoapClient();
client.Endpoint.Address = "site url"+"/_vti_bin/lists.asmx";

How to add search.asmx web reference to webpart application in VS2008 ? facing problem

I am trying to create the custom search webpart. I want to use Enterprise Search Query Model (MOSS) . But unable to add the web reference of the search.asmx webservice. infact it has no WSDL file. just wondering about what setting it needs.... too If anybody know how to do, same. please let me know. you can find the search webservice at
Edited : "http://server_name:port_no/sites/site_name/_vti_bin/search.asmx"
I tried by right cliking on project said Add Service Reference , inserts the url said Go , but nothis was there. same i tried with web aplication . but no effect. even i can see the methods in browser, but cannot generate WSDL ??
You are using the wrong url.
http://PORTAL/_vti_bin/spsearch.asmx is for WSS3.
http://PORTAL/_vti_bin/search.asmx is for MOSS.
Also check out a test tool for searching SharePoint 2007.
Problem solved. browse searchwsdl.aspx in browser. it generates the wsdl (xml) format file. just give the web reference to the application. that it.

How can I get started using a WSDL file with Visual Studio it's in the root of the project instead of being hosted on the internet somewhere

I've been tasked when integrating a web form into Oracle CRM on Demand (Siebel) using web services. I've been given the WSDL, and some high level documentation from Oracle.
Well, I knew I was in trouble when I tried to add the WSDL as Web Reference and I was asked to enter an URL. I have the WSDL file in the root of the project, but I have no idea how to link to it.
So, I guess that means I need to learn up on web services using C# and Visual Studio. I have a Safari Books Online account, so I can look up most anything.
It's been a while, but I'm OK at the form part. I just need help connecting and using the web service.
Edit #1: Ok, to clarify my question: I am well aware on how to use web services in general. My specific question is how take this WSDL file and do something with it. If the WSDL was hosted somewhere else, I could just add it as a Web Reference. But, I have the file in the project itself and that is what I am having problems with.
The web reference asks for a URL but you can point it to a local file. Just paste the local file path of your WSDL in and it should work.
Further Clarification of Web Reference URL vs URL to access Web Service
Web Reference URL is used to generate and update wrapper classes for WSDL/Web Service. It is not the URL used at runtime to access Web Service.
URL property on generated wrapper classes is used to access actual web service at runtime.
Update:
It should add a path in the web.config or app.config/settings file (Depending on your project type) similiar to the following:
<setting name="Namespace_WebReferenceName" serializeAs="String">
<value>XXX</value>
</setting>
Which should map to a URL property in the generated web reference wrapper classes. You can modify the URL property programmatically to point wherever you want:
Dim shipService As ShipService = New ShipService() 'Initialize the service - where ShipService is the Generated WebReference Wrapper CLass
shipService.Url = ConfigurationSettings.AppSettings("FedExOnlineURL")

Resources