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>?













1















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?










share|improve this question


























    1















    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?










    share|improve this question
























      1












      1








      1








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 20:32









      user3266638user3266638

      14013




      14013






















          2 Answers
          2






          active

          oldest

          votes


















          1














          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






          share|improve this answer






























            0














            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.






            share|improve this answer






















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



              );













              draft saved

              draft discarded


















              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









              1














              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






              share|improve this answer



























                1














                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






                share|improve this answer

























                  1












                  1








                  1







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 20:46









                  douglas.kirschmandouglas.kirschman

                  7115




                  7115























                      0














                      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.






                      share|improve this answer



























                        0














                        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.






                        share|improve this answer

























                          0












                          0








                          0







                          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.






                          share|improve this answer













                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 7 at 22:37









                          Scott HannenScott Hannen

                          13.2k1426




                          13.2k1426



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                              Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                              2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived