question

RyanGeorge-0549 avatar image
0 Votes"
RyanGeorge-0549 asked BruceBarker-8516 answered

How do I embed a 3rd Party API service inside my ASP.net Blazor Web Assembly

Hello,

I was in the process of making a website that features a UI displaying information from a 3rd Party API

When the client refreshes the page: Client Browser requests XML from 3rd Party API -> Client then parses XML Response -> Client then outputs the data (All Asynchronously)

As far as I have read in the Microsoft Docs they set up their own API service using their own data from the server, but since my XML is rather large calling an API which then calls another API would result in a large download time overall..

How should I implement the request to a 3rd Party API directly?

I have fully developed the backend already in C#, the request looks something like this:

public string XMLRequest;

 public static async Task<string> executeRequest()
         {
    
             HttpWebRequest client = (HttpWebRequest)WebRequest.Create(3RDPARTYURL);
    
             client.ContentType = "application/soap+xml";
    
             client.Method = "POST";
             using (Stream postStream = await client.GetRequestStreamAsync())
             {
                 byte[] postBytes = Encoding.ASCII.GetBytes(XMLRequest);
                 await postStream.WriteAsync(postBytes, 0, postBytes.Length);
                 await postStream.FlushAsync();
             }
                
    
             Task<string> Response;
             using (var response = (HttpWebResponse)await client.GetResponseAsync())
             using (Stream streamResponse = response.GetResponseStream())
             using (StreamReader streamReader = new StreamReader(streamResponse))
             {
                 Response = streamReader.ReadToEndAsync();
             }
    
             return await Response;
    
 }



Ryan

dotnet-aspnet-core-webapi
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The standard approach for invoking a SOAP service is creating a service reference from the WSDL. The service reference generates code that allows you to access the remote SOAP service as if the service is a local API. Plus you get to use strong types rather than manually serializing XML/SOAP messages.

I would create a Web API proxy. The Blazor WASM application calls Web API proxy. Web API invokes the SOAP service and returns the strong type. Most likely, you will not be able to invoke the SOAP service directly from Blazor WASM due to the browser's cross origin policy (CORS). See the SOAP service documentation.


0 Votes 0 ·

@RyanGeorge-0549 I think it should not be able to achieve what you want. The link you provide, the content above only tells us that we can call resources across sites and allow your webapp to access third-party content. My suggestion is to access the 3rd Party API in this area of your webapp application. 1. Client Browser requests content from Redis -> 2. Redis Get/Update from 3rd Party API. This will save your request access time and will not cause you to send Http requests to the 3rd Party API every time, and then download the xml file .

0 Votes 0 ·

1 Answer

BruceBarker-8516 avatar image
0 Votes"
BruceBarker-8516 answered

the main requirement is the the 3rd party url support CORS for your site. because the browser will restrict access (as blazor is just using javascript to make ajax calls). if the 3rd party does supports CORS, then you will need a server proxy.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.