Can I use a web api from a web application as a service reference?Copy the entire contents of a directory in C#ASP.NET Web Site or ASP.NET Web Application?How can I get the application's path in a .NET console application?Metadata file '.dll' could not be foundPass Method as Parameter using C#How do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?Web Reference vs. Service ReferenceWCF vs ASP.NET Web APIPass an array of integers to ASP.NET Web API?Why not inherit from List<T>?
Calculate Pi using Monte Carlo
Connection Between Knot Theory and Number Theory
Can you take a "free object interaction" while incapacitated?
Center page as a whole without centering each element individually
Weird lines in Microsoft Word
What do the positive and negative (+/-) transmit and receive pins mean on Ethernet cables?
1 John in Luther’s Bibel
How do you say "Trust your struggle." in French?
PTIJ: Which Dr. Seuss books should one obtain?
Relations between homogeneous polynomials
What can I do if I am asked to learn different programming languages very frequently?
Does capillary rise violate hydrostatic paradox?
A seasonal riddle
Why didn't Voldemort know what Grindelwald looked like?
Make a Bowl of Alphabet Soup
Writing in a Christian voice
Hashing password to increase entropy
If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?
Started in 1987 vs. Starting in 1987
How to get directions in deep space?
Non-Borel set in arbitrary metric space
What is the tangent at a sharp point on a curve?
Is there a distance limit for minecart tracks?
How would a solely written language work mechanically
Can I use a web api from a web application as a service reference?
Copy the entire contents of a directory in C#ASP.NET Web Site or ASP.NET Web Application?How can I get the application's path in a .NET console application?Metadata file '.dll' could not be foundPass Method as Parameter using C#How do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?Web Reference vs. Service ReferenceWCF vs ASP.NET Web APIPass an array of integers to ASP.NET Web API?Why not inherit from List<T>?
Is it possible to add as a reference and call an APIs controller methods as a service on another project? What are the alternatives if this is not possible?
c# .net asp.net-mvc-5
add a comment |
Is it possible to add as a reference and call an APIs controller methods as a service on another project? What are the alternatives if this is not possible?
c# .net asp.net-mvc-5
add a comment |
Is it possible to add as a reference and call an APIs controller methods as a service on another project? What are the alternatives if this is not possible?
c# .net asp.net-mvc-5
Is it possible to add as a reference and call an APIs controller methods as a service on another project? What are the alternatives if this is not possible?
c# .net asp.net-mvc-5
c# .net asp.net-mvc-5
asked Mar 7 at 20:32
user3266638user3266638
14013
14013
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Web API types of applications do not have a 'service reference' anymore. They do not produce WSDL, so you cannot add them like you used to do with SOAP services. No proxy classes are generated... no intelli-sense.
Web APIs are typically called with lightweight http requests and return JSON and not XML based SOAP responses like traditional ASMX or SVC (WCF) services.
You have some reading to do I believe.
To answer your question, you CAN indeed call API services from a web application (say a controller method in an MVC app), but you won't have proxy classes to help you.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
add a comment |
When you create a service reference you end up with a reference to an interface and a client class that implements the interface.
You can follow pretty much the same pattern without a WCF service reference. In fact, that's one of the benefits of depending on an interface. It doesn't matter to your application whether the implementation is a call to a WCF service, an API, or anything else.
First declare an interface that describes how you will interact with the API.
public interface ISomethingService
public SomeData GetSomeData(string id);
That interface is what your other classes depend on. They'll never know what the implementation is.
Your implementation could be something like this. I'm using RestSharp to create the API client because I like it better than managing an HttpClient
:
public class SomethingServiceApiClient : ISomethingService
private readonly string _baseUrl;
public SomethingServiceApiClient(string baseUrl)
_baseUrl = baseUrl;
public SomeData GetSomeData(string id)
var client = new RestClient(_baseUrl);
var request = new RestRequest($"something/id", Method.POST);
var response = client.Execute<SomeData>(request);
return response.Data;
In your startup you would register this class as the implementation of ISomethingService
and pass the base url from configuration. That would also allow you to pass a different url for development, production, etc. if needed.
Ultimately it's no different from depending on a WCF service. One difference is that a WCF service defines an interface, but in this case you have to do it. That's actually a good thing, because it's better for your application to define its own interface rather than directly depending on the ones someone else provides. You can wrap their interface or API in a class that implements your own interface, giving you control over the interface you depend on.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052353%2fcan-i-use-a-web-api-from-a-web-application-as-a-service-reference%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Web API types of applications do not have a 'service reference' anymore. They do not produce WSDL, so you cannot add them like you used to do with SOAP services. No proxy classes are generated... no intelli-sense.
Web APIs are typically called with lightweight http requests and return JSON and not XML based SOAP responses like traditional ASMX or SVC (WCF) services.
You have some reading to do I believe.
To answer your question, you CAN indeed call API services from a web application (say a controller method in an MVC app), but you won't have proxy classes to help you.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
add a comment |
Web API types of applications do not have a 'service reference' anymore. They do not produce WSDL, so you cannot add them like you used to do with SOAP services. No proxy classes are generated... no intelli-sense.
Web APIs are typically called with lightweight http requests and return JSON and not XML based SOAP responses like traditional ASMX or SVC (WCF) services.
You have some reading to do I believe.
To answer your question, you CAN indeed call API services from a web application (say a controller method in an MVC app), but you won't have proxy classes to help you.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
add a comment |
Web API types of applications do not have a 'service reference' anymore. They do not produce WSDL, so you cannot add them like you used to do with SOAP services. No proxy classes are generated... no intelli-sense.
Web APIs are typically called with lightweight http requests and return JSON and not XML based SOAP responses like traditional ASMX or SVC (WCF) services.
You have some reading to do I believe.
To answer your question, you CAN indeed call API services from a web application (say a controller method in an MVC app), but you won't have proxy classes to help you.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Web API types of applications do not have a 'service reference' anymore. They do not produce WSDL, so you cannot add them like you used to do with SOAP services. No proxy classes are generated... no intelli-sense.
Web APIs are typically called with lightweight http requests and return JSON and not XML based SOAP responses like traditional ASMX or SVC (WCF) services.
You have some reading to do I believe.
To answer your question, you CAN indeed call API services from a web application (say a controller method in an MVC app), but you won't have proxy classes to help you.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
answered Mar 7 at 20:46
douglas.kirschmandouglas.kirschman
7115
7115
add a comment |
add a comment |
When you create a service reference you end up with a reference to an interface and a client class that implements the interface.
You can follow pretty much the same pattern without a WCF service reference. In fact, that's one of the benefits of depending on an interface. It doesn't matter to your application whether the implementation is a call to a WCF service, an API, or anything else.
First declare an interface that describes how you will interact with the API.
public interface ISomethingService
public SomeData GetSomeData(string id);
That interface is what your other classes depend on. They'll never know what the implementation is.
Your implementation could be something like this. I'm using RestSharp to create the API client because I like it better than managing an HttpClient
:
public class SomethingServiceApiClient : ISomethingService
private readonly string _baseUrl;
public SomethingServiceApiClient(string baseUrl)
_baseUrl = baseUrl;
public SomeData GetSomeData(string id)
var client = new RestClient(_baseUrl);
var request = new RestRequest($"something/id", Method.POST);
var response = client.Execute<SomeData>(request);
return response.Data;
In your startup you would register this class as the implementation of ISomethingService
and pass the base url from configuration. That would also allow you to pass a different url for development, production, etc. if needed.
Ultimately it's no different from depending on a WCF service. One difference is that a WCF service defines an interface, but in this case you have to do it. That's actually a good thing, because it's better for your application to define its own interface rather than directly depending on the ones someone else provides. You can wrap their interface or API in a class that implements your own interface, giving you control over the interface you depend on.
add a comment |
When you create a service reference you end up with a reference to an interface and a client class that implements the interface.
You can follow pretty much the same pattern without a WCF service reference. In fact, that's one of the benefits of depending on an interface. It doesn't matter to your application whether the implementation is a call to a WCF service, an API, or anything else.
First declare an interface that describes how you will interact with the API.
public interface ISomethingService
public SomeData GetSomeData(string id);
That interface is what your other classes depend on. They'll never know what the implementation is.
Your implementation could be something like this. I'm using RestSharp to create the API client because I like it better than managing an HttpClient
:
public class SomethingServiceApiClient : ISomethingService
private readonly string _baseUrl;
public SomethingServiceApiClient(string baseUrl)
_baseUrl = baseUrl;
public SomeData GetSomeData(string id)
var client = new RestClient(_baseUrl);
var request = new RestRequest($"something/id", Method.POST);
var response = client.Execute<SomeData>(request);
return response.Data;
In your startup you would register this class as the implementation of ISomethingService
and pass the base url from configuration. That would also allow you to pass a different url for development, production, etc. if needed.
Ultimately it's no different from depending on a WCF service. One difference is that a WCF service defines an interface, but in this case you have to do it. That's actually a good thing, because it's better for your application to define its own interface rather than directly depending on the ones someone else provides. You can wrap their interface or API in a class that implements your own interface, giving you control over the interface you depend on.
add a comment |
When you create a service reference you end up with a reference to an interface and a client class that implements the interface.
You can follow pretty much the same pattern without a WCF service reference. In fact, that's one of the benefits of depending on an interface. It doesn't matter to your application whether the implementation is a call to a WCF service, an API, or anything else.
First declare an interface that describes how you will interact with the API.
public interface ISomethingService
public SomeData GetSomeData(string id);
That interface is what your other classes depend on. They'll never know what the implementation is.
Your implementation could be something like this. I'm using RestSharp to create the API client because I like it better than managing an HttpClient
:
public class SomethingServiceApiClient : ISomethingService
private readonly string _baseUrl;
public SomethingServiceApiClient(string baseUrl)
_baseUrl = baseUrl;
public SomeData GetSomeData(string id)
var client = new RestClient(_baseUrl);
var request = new RestRequest($"something/id", Method.POST);
var response = client.Execute<SomeData>(request);
return response.Data;
In your startup you would register this class as the implementation of ISomethingService
and pass the base url from configuration. That would also allow you to pass a different url for development, production, etc. if needed.
Ultimately it's no different from depending on a WCF service. One difference is that a WCF service defines an interface, but in this case you have to do it. That's actually a good thing, because it's better for your application to define its own interface rather than directly depending on the ones someone else provides. You can wrap their interface or API in a class that implements your own interface, giving you control over the interface you depend on.
When you create a service reference you end up with a reference to an interface and a client class that implements the interface.
You can follow pretty much the same pattern without a WCF service reference. In fact, that's one of the benefits of depending on an interface. It doesn't matter to your application whether the implementation is a call to a WCF service, an API, or anything else.
First declare an interface that describes how you will interact with the API.
public interface ISomethingService
public SomeData GetSomeData(string id);
That interface is what your other classes depend on. They'll never know what the implementation is.
Your implementation could be something like this. I'm using RestSharp to create the API client because I like it better than managing an HttpClient
:
public class SomethingServiceApiClient : ISomethingService
private readonly string _baseUrl;
public SomethingServiceApiClient(string baseUrl)
_baseUrl = baseUrl;
public SomeData GetSomeData(string id)
var client = new RestClient(_baseUrl);
var request = new RestRequest($"something/id", Method.POST);
var response = client.Execute<SomeData>(request);
return response.Data;
In your startup you would register this class as the implementation of ISomethingService
and pass the base url from configuration. That would also allow you to pass a different url for development, production, etc. if needed.
Ultimately it's no different from depending on a WCF service. One difference is that a WCF service defines an interface, but in this case you have to do it. That's actually a good thing, because it's better for your application to define its own interface rather than directly depending on the ones someone else provides. You can wrap their interface or API in a class that implements your own interface, giving you control over the interface you depend on.
answered Mar 7 at 22:37
Scott HannenScott Hannen
13.2k1426
13.2k1426
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052353%2fcan-i-use-a-web-api-from-a-web-application-as-a-service-reference%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown