Base solution for your next web application
Open Closed

How to call AppService methods from Windows desktop app #986


User avatar
0
otgontugsmiimaa created

Hi, Halil

An user should login from Windows desktop app and can manage permitted objects. For instance, I want to call IOrganizationUnitAppService.GetOrganizationUnits() function from Windows desktop application.

To accomplish it, I can use JSON to get/post data from/to AppService. In this case, i have to redefine every DTO in client app. But i want to reuse DTOs in which AppService. Is it possible?

I've seen AbpEfConsoleApp sample code. There, i've to put database connection string in app.config. And i don't want to make my database access public.

Thank you


6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    It's possible.

    You can add reference to your Core and Application assemblies from the desktop application amd make remote requests and map to DTOs defined in the Application assembly.

    But this way, you distribute your DTOs within desktop client application. If this is not a problem, then go to this way. If it's problem (clients may use some reverse engineering tool to discover your source code, but surely can not reach to db), then create a new class library project, move DTOs to that assembly and share this assembly between applications.

  • User Avatar
    0
    otgontugsmiimaa created

    Yes, it might be a problem. So the solution is to move DTOs to a shared library. Thank you. Some more clarifications on class library please. Do i have to IOrganizationService interface to the class library and leave its implementation in Application project? So i can reuse the interface in client app. By the way, where can i configure URL to hosted app service?

  • User Avatar
    0
    otgontugsmiimaa created

    The DTOs depend on ASPNETZero interfaces such as IPagedAndSortedInputDto ... If i reference them, there would be no sense separating into class library project.

  • User Avatar
    0
    hikalkan created
    Support Team

    You can move also IPagedAndSortedInputDto to this DTO class library. And reference this library from your web and desktop applications.

  • User Avatar
    0
    otgontugsmiimaa created

    I moved interfaces such as IPagedAndSortedInputDto to a new class library and referenced it to all projects. The code below may need someone to call Webapi functions. Thank you.

    public static string BASE_URL = "http://localhost:6240";
            public static string GET_PARTS_URL = "api/services/app/part/GetParts";
    
            public static ListResultOutput<PartDto> GetParts()
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(BASE_URL);
    
                // Add an Accept header for JSON format.
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                string serviceUrl;
    
                // 2. Get all employees.
                serviceUrl = GET_PARTS_URL;
                AjaxResponse<ListResultOutput<PartDto>> parts = null;
                HttpResponseMessage response = client.PostAsJsonAsync(serviceUrl, new GetPartsInput()).Result;        // Blocking call!
                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body. Blocking!
                    //parts = response.Content.ReadAsAsync<AjaxResponse<ListResultOutput<PartListDto>>>().Result;
                    string s = response.Content.ReadAsStringAsync().Result;
                    parts = JsonConvert.DeserializeObject<AjaxResponse<ListResultOutput<PartDto>>>(s);
                }
                else
                {
                    throw new Exception(string.Format("{0} {1}", (int)response.StatusCode, response.ReasonPhrase));
                }
                return parts.Result;
            }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Thank you for sharing your experince. You can also check this code: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/blob/master/ConsoleRemoteWebApiCall/CallApiFromConsole/Program.cs">https://github.com/aspnetboilerplate/as ... Program.cs</a> It may make easier to call a remote app service method.