Referencing my Web API in the Xamarin forms project

PierreParmPierreParm FRMember ✭✭

Hi !
I guess this is a classic C# issue but I think it is still more adapted to ask here.
I have a Xamarin forms project and a web API project, both in the same solution.

My goal is to make calls to that API and deserialize the response to get my objects (of course).
Do to do, I don't want to copy/paste the model and simply use it in the Xamarin project.

Problem is that I can't use the namespace in the Xamarin forms project and I can't add a reference to the web API in the Xamarin project.

Has anyone experienced this issue ? Any ideas on how I can use my web API model in the Xamarin project ?
Thanks !

Best Answer

  • NMackayNMackay GB mod
    Accepted Answer

    Well you'd have to refactor things a little.

    One way is to create a PCL library both your service API and your Xamarin PCL app (assuming your using PCL) can share and just reference the DTO's in the shared library.

    Our app consumes several restful endpoints, I just ended up creating a MyApp.Data project that had the service interfaces and implementation and DTO's for each service, it's a little extra work but as we consume a 3rd party REST service as well as our end Restful services we took this approach.

    It may be copy your classes into Notepad++ and just refactor the namespaces to match your app, create a library or a model folder in your app and manually add the class files to the correct location, it's 5 minutes work.

Answers

  • NMackayNMackay GBInsider, University mod
    Accepted Answer

    Well you'd have to refactor things a little.

    One way is to create a PCL library both your service API and your Xamarin PCL app (assuming your using PCL) can share and just reference the DTO's in the shared library.

    Our app consumes several restful endpoints, I just ended up creating a MyApp.Data project that had the service interfaces and implementation and DTO's for each service, it's a little extra work but as we consume a 3rd party REST service as well as our end Restful services we took this approach.

    It may be copy your classes into Notepad++ and just refactor the namespaces to match your app, create a library or a model folder in your app and manually add the class files to the correct location, it's 5 minutes work.

  • PierreParmPierreParm FRMember ✭✭

    All right, that is what I "feared" but that is indeed not a lot of work to do.
    It was more curiosity than really not wanting to do that.

    Thanks a lot for the answer !

Sign In or Register to comment.