Trying to set up ServiceStack with OrmLite to connect to my local SQL instance. Getting error
"Declaration referenced in a method implementation cannot be a final
method"
and it's driving me nuts. Here are my steps so far:
New "ServiceStack ASP.NET Empty" project
In the "Service" project, installed ServiceStack.OrmLite.SqlServer
NuGet package
Added the following code to the AppHost "Configure" section:
public override void Configure(Funq.Container container)
{
var connectionString = ConfigurationManager.ConnectionStrings["ApiDbConnectionString"].ConnectionString;
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
OrmLiteConnectionFactory dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);
}
Here are my using statements:
using System;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.SqlServer;
using LRIService.ServiceInterface;
I would like to use this database connection throughout the app for caching, data access, and eventually user authentication.
Lastly, are there any good examples of ServiceStack running as a standalone service (not as part of an ASP.NET MVC app)? I am going to be accessing this via a stand-alone AngularJS app that may or may not be hosted on the same domain (I've worked with the CORS feature before). I'm just having trouble separating the ASP.NET MVC stuff from the ServiceStack stuff in many of the examples on the web.
Thanks for any help...
Your error message sounds like you're mixing different versions of ServiceStack together. Try clearing your NuGet packages Cache. Alternatively you can try the most recent v4.0.34 packages on MyGet.
As for non ASP.NET MVC Examples, most of the ServiceStack Live Demos are Single Page Apps that don't use ASP.NET MVC at all:
Chat (jQuery + ss-utils.js)
Entire App ILMerged into a single cross-platform Chat.exe (Self Host)
React Chat (port to React.js)
Http Benchmarks (jQuery + ss-utils.js)
Email Contacts (jQuery + ss-utils.js)
Stack API's (Angular JS)
Imgur (jQuery)
Todos (Backbone.js)
Razor Rockstars
ASP.NET host
A Stand-alone, self-hosted HttpListener (Self Host)
A Stand-alone Windows Service (Self Host)
Self-hosted HttpListener with
Entire App ILMerged into a single cross-platform App.exe
Hosted inside WinForms with Chromium Embedded Framework
Hosted inside Mac OSX Cocoa App with Xmarain.Mac
REST Files (jQuery)
Redis StackOverflow (jQuery)
Related
I am currently trying to make a request to a third party library in my ASP.NET Core 6 Web API and work with this data (it really has to run over a Web API template).
That means I'm importing data from another API.
Unfortunately I don't know how to tell my application to make a call.
I would have expected the Task to run automatically when the application starts. Unfortunately this is not the case.
I first wanted to test whether this works at all when the application is started. Later, I would build in a scheduler, which sends requests accordingly.
It should be possible, right?
It would be great if someone could tell me as well if it's possible to put the URL "localhost:xxx/" in the constructor somehow, but still not get any dependency injection errors with AddScoped.
I use Flurl.Http to make Http Requests.
If it is important. My program.cs is in a console application and DataImport in an empty project
Unfortunately, I am relatively new to the ASP.NET world and I hope that the question is not too unprofessional. Otherwise I apologize. It's kind of hard to google for a problem like this and find something
using Flurl;
using Flurl.Http;
public class DataImport
{
private readonly Service service;
public DataImport(Servie service)
{
_service = service;
}
public async Task<IEnumerable<Data>> ImportData()
{
var data = await "localhost:xxx/".AppendPathSegment("data").GetJsonAsync<DataDto[]>();
return _service.Add(data.Select(it => new DatoDtoToData(it)));
}
}
program.cs:
builder
.Services
... Service Injections
....
.AddScoped<DataImport>()
I would encourage you to look into Azure Functions.
Have a look at Microsoft's Introduction to Azure Functions. In the Scenarios section we can read.
The following are a common, but by no means exhaustive, set of scenarios for Azure Functions.
If you want to...
then...
Build a web API
Implement an endpoint for your web applications using the HTTP trigger
...
Build a serverless workflow
Chain a series of functions together using durable functions
...
Run scheduled tasks
Execute code on pre-defined timed intervals
...
I really think this could work well for you because:
You can run/host it locally or host it in Azure.
It's very easy to start with. Microsoft's tutorials are:
Quickstart: Create your first C# function in Azure using Visual Studio, and
Quickstart: Create a C# function in Azure using Visual Studio Code
It supports multiple ways of triggering your functions. There are over 20 tiggers; with 2 of the most relevant for you should be:
Azure Functions HTTP trigger
Timer trigger for Azure Functions
Recently our team has decided to implement micro front end architecture in our legacy product. It has been developed using Asp.Net aspx pages along with javascript/jquery.
Last year we started using angular in our application for some of the views. To load angular we are placing the prod build files in .net project and we are loading the component in aspx master page.
We are planning to migrate our rest pending older views to angular using micro front end architecture.
So I did a small poc for the same and was able to achieve the architecture to somewhere close to it.
I followed this url for implementation and ran it on port 4400.
https://medium.com/swlh/build-micro-frontends-using-angular-elements-the-beginners-guide-75ffeae61b58
And in my existing angular project i am loading this using customElements
this.appendCustomElementWithUrls('app-positions','http://localhost:4400/main-es5.js', (<HTMLElement>document.getElementById("chartAppContainerNamInqA")) );
appendCustomElementWithUrls(name: string,url: string,target: HTMLElement){
if (!customElements.get(name)) {
const script = document.createElement('script');
script.src = url;
document.head.appendChild(script);
}
const component = document.createElement(name);
target.appendChild(component);
}
And this works as expected and I am able to load customElements in my dev env. But for production I am really not sure how to implement.
My concern:
Will I have to run app on some port in prod as well? If yes how to do that and can it be dynamic such that user has ability to change the port. The way we have in .net application. Since client may have something already running on that port
The way I am trying to achieve is correct or not.
Thanks in advance.
For those who might have requirement like this.
I did lot of research and went through lot of articles and came out with a solution.
So I created a separate application using Angular elements and generated single bundle using cmd;
ng build --prod --output-hashing none --single-bundle true
then I created an application in IIS and placed all the prod generated files in it on port 9091. You can use any port for that.
In my web.config file I created a key such that if user changes the port number then they directly update web.config:
<add key="MicroFrontEnd" value="http://localhost:9190"/>
Since port should be configurable so I created an api to fetch the port number.
Then I used this in my shell app and it works like a charm.
I am creating an application which is going to use windows authentication to allow access to page based on user and its roles. I have gone ahead and configured IIS to use windows authentication and disabled anonymous access. I am able to browse the application without any issue and I have successfully enabled windows authentication for the application.
Now I am trying to set access to controller level by using Authorize, but I am unable to figure it out how it works with ASP.net 5. I am completely new to ASP / C# programming platform but I did search online resources and I came across this and I used below example to give it a try but I get red squiggly line under users and I think ASP.NET 5 is unable to find users. I also made sure that I have added all the references at the top.
[Authorize(Users = #"CONTOSO\Rick, CONTOSO\Keith, CONTOSO\Mike")]
References:
using Microsoft.AspNet.Mvc;
using RMDAAutomation.Models.Repository;
using RMDAAutomation.Models.Entity;
using Microsoft.AspNet.Authorization;
Any suggestions is highly appreciated. :)
UPDATE 1:
TutorialsController.cs:
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Authorization;
namespace RMDAAutomation.Controllers.Web
{
public class TutorialsController : Controller
{
[Authorize(Roles = #"DOMAIN\GROUP")]
public IActionResult Index()
{
return View();
}
}
}
Tutorial Index View:
<div class="container">
<h1>Tutorials</h1>
</div>
Visual studio project settings:
using System.Web.Mvc;
This using solves the issue in my project
just starting to create an API to my a web application using the ASP.NET MVC4 Web API project template. http://www.asp.net/mvc/mvc4
No problems with the API so far, but I was about to write a small C# app to test the API.
Almost all the sample I can find is using the a class called HttpClient.
Where can I find the HttpClient and how do I install it?
Rather than using the build in HttpClient class of the .NET framework which has a lot of issues when dealing with StatusCodes that are different than the expected ones. I recommend using a library called RestSharp.
It has become .NET Http/Rest client of choice, you can get it here: http://restsharp.org/
It is a very powerful library that is perfectly suited to do what you want.
It's on nuget, search for HttpClient
http://nuget.org/packages/System.Net.Http
Use WebRequest as described here
// Create a new 'Uri' object with the specified string.
Uri myUri =new Uri("http://www.contoso.com");
// Create a new request to the above mentioned URL.
WebRequest myWebRequest= WebRequest.Create(myUri);
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse= myWebRequest.GetResponse();
If its a REST interface use RestSharp but you would need XSD first.
If the class is not available from your code, then you could download it from a NuGet package, like described in the article:
http://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee
or you can try to locate it inside the namespace: System.Net.Http
There is also an example for you wich should get you started!
I am trying to implement webservices in my porject but the reference can't be found. What might be the issue here? What am I missing?
This is how I reference the Web service. I went to the project root in the solution explorer. Right click and Add Web Reference. It showed the method and everything but when I go in the code to make reference I can't find it.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using net.webservicex.www;
I found the problem. You need to create a new website in order to show all the elements from the webservice. Creating a new Project was not giving me the expected results to be able to connect to the webservice.
I'm a little confused by the comments under the question...
If you are using a web-reference (2.0), you are asked for a "web reference name"; this doubles as your namespace to the item. You can access the service via something like MyWebReferenceName.MyServiceName.
When you use the "add service reference" dialog (3.0+), you are prompted for a "namespace", which acts identically. In this case, the service is available at something like MyWebServiceReferenceName.MyServiceNameClient.