To set the IO path to the current:
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
Easy? Enjoy your code :D
วันจันทร์ที่ 3 กรกฎาคม พ.ศ. 2560
วันจันทร์ที่ 12 มิถุนายน พ.ศ. 2560
IIS connect to SQL server
To develop and deply the web service on IIS. In case your service have to connect to SQL server on the local machine. Please follow these steps.
1. Setting SQL server to allow log in by user and Windows authentication
- Open SQL Server Management Studio & log in with Windows Authentication
- On "Object Explorer", right click on the SQL Server and click "Properties"
- On "Security" tab, section "Server authentication" select "SQL Server and Windows Authentication mode"
- Click OK
2. Restart SQL Server
- Command line "Services"
- Find "SQL Server [SQLEXPRESS]"
- Right click and select "Restart"
3. Create log in user for SQL Server authentication
- Open SQL Server Management Studio & log in
- On "Object Explorer", expand the "Security"
- Right click on "Logins", select "New Login..."
- Enter login name and setting password
- On "User Mapping", check the target database
- Click OK
4. Set Web.config
- Use this configuration
<connectionStrings>
<add name="[YourPreferDatabaseConfigName]" connectionString="Data Source=[Domain]\SQLEXPRESS;Initial Catalog=[Database];Password=[password];User ID=[username];" providerName="System.Data.SqlClient" />
</connectionStrings>
....Everything will be alright....
Thanks for
- https://msdn.microsoft.com/en-us/library/bb986870.aspx
- https://msdn.microsoft.com/en-us/library/aa337562(v=sql.105).aspx
- https://stackoverflow.com/questions/20923015/login-to-microsoft-sql-server-error-18456
- https://stackoverflow.com/questions/27387912/keyword-not-supported-provider-opening-sqlconnection
1. Setting SQL server to allow log in by user and Windows authentication
- Open SQL Server Management Studio & log in with Windows Authentication
- On "Object Explorer", right click on the SQL Server and click "Properties"
- On "Security" tab, section "Server authentication" select "SQL Server and Windows Authentication mode"
- Click OK
2. Restart SQL Server
- Command line "Services"
- Find "SQL Server [SQLEXPRESS]"
- Right click and select "Restart"
3. Create log in user for SQL Server authentication
- Open SQL Server Management Studio & log in
- On "Object Explorer", expand the "Security"
- Right click on "Logins", select "New Login..."
- Enter login name and setting password
- On "User Mapping", check the target database
- Click OK
4. Set Web.config
- Use this configuration
<connectionStrings>
<add name="[YourPreferDatabaseConfigName]" connectionString="Data Source=[Domain]\SQLEXPRESS;Initial Catalog=[Database];Password=[password];User ID=[username];" providerName="System.Data.SqlClient" />
</connectionStrings>
....Everything will be alright....
Thanks for
- https://msdn.microsoft.com/en-us/library/bb986870.aspx
- https://msdn.microsoft.com/en-us/library/aa337562(v=sql.105).aspx
- https://stackoverflow.com/questions/20923015/login-to-microsoft-sql-server-error-18456
- https://stackoverflow.com/questions/27387912/keyword-not-supported-provider-opening-sqlconnection
วันอังคารที่ 14 มีนาคม พ.ศ. 2560
Create API doc by HelpPage
Follow the steps from microsoft are enough if your project incudes the file 'Global.asax'. but if it does not, neither me, you have to register the callback for GlobalConfiguration. Due to HelpPage requires to register the global configuration.
Add this line to your startup.cs file
// Register callback for GlobalConfiguration
GlobalConfiguration.Configure(WebApiConfig.Register);
Done. Hope this help (me).
Add this line to your startup.cs file
// Register callback for GlobalConfiguration
GlobalConfiguration.Configure(WebApiConfig.Register);
Done. Hope this help (me).
วันจันทร์ที่ 27 กุมภาพันธ์ พ.ศ. 2560
Async-Await CSVHelper - CSVWriter
I want to write and send the CSV file over network stream (response to GET service). I familiar with CSVHelper, which provides function to read and write CSV file, I apply it to my project.
Unfortunately, the service throw me the some exceptions. I googled it and found this issue in JonhClose GitHub, John is the creator! He did not put this feature to avoid blocking task.
I try to apply CSVHelperAsync to my project. But it does not work for me.
Then,, why I write this blog??? I want to tel you that the best way to do is to generate the CSV yourself and attach it to the response Message.
[HttpGet]
[Route("DownloadCSV")]
public HttpResponseMessage GetCSVContentInfoByProduct(int productId)
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
MemoryStream stream = _business.GetCSV();
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{ FileName = "myCsv.csv" };
return response;
}
public MemoryStream GetCSV()
{
MemoryStream stream = new MemoryStream();
string csv = <<your_csv_string>>;
StreamWriter writer = new StreamWriter(stream);
writer.Write(csv);
writer.Flush();
stream.Position = 0;
return stream;
}
Have fun :D
Unfortunately, the service throw me the some exceptions. I googled it and found this issue in JonhClose GitHub, John is the creator! He did not put this feature to avoid blocking task.
I try to apply CSVHelperAsync to my project. But it does not work for me.
Then,, why I write this blog??? I want to tel you that the best way to do is to generate the CSV yourself and attach it to the response Message.
[HttpGet]
[Route("DownloadCSV")]
public HttpResponseMessage GetCSVContentInfoByProduct(int productId)
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
MemoryStream stream = _business.GetCSV();
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{ FileName = "myCsv.csv" };
return response;
}
public MemoryStream GetCSV()
{
MemoryStream stream = new MemoryStream();
string csv = <<your_csv_string>>;
StreamWriter writer = new StreamWriter(stream);
writer.Write(csv);
writer.Flush();
stream.Position = 0;
return stream;
}
Have fun :D
Cannot use string as an type-specific constraints
I am a starter in Web API. I try to implement GET service and use string as a constraint. VS throw me the error that.
------------------------------
Server Error in '/' Application.
Exception Details: System.InvalidOperationException: The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'.
------------------------------
Server Error in '/' Application.
The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.InvalidOperationException: The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'.
วันอาทิตย์ที่ 22 มกราคม พ.ศ. 2560
The underlying provider failed on Open.
I tried to connect the database on my local machine. API throws exception "The underlying provider failed on Open". I checked the inner exception to find out more my fault.
The solution key is in your Web.config file. I use this configuration and it works.
<connectionStrings>
<add name="MyDBEntities" connectionString="metadata=res://*/DBAAA.csdl|res://*/DBAAA.ssdl|res://*/DBAAA.msl;provider=System.Data.SqlClient;
provider connection string="
data source=MyMachineDomainHQGWD2S\SQLEXPRESS;
initial catalog=My_DB_TEST;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Hope it can help :D
======================================================================
UPDATE
======================================================================
I've got the problem again! I want to add the strongly note that YOU CANNOT SET data source AS YOU LOCAL IP!! PLEASE SET IT TO $DOMAIN\$MACHINE_NAME
Then you can access the database with locahost:port/service or _your_ip:port/service
======================================================================
The solution key is in your Web.config file. I use this configuration and it works.
<connectionStrings>
<add name="MyDBEntities" connectionString="metadata=res://*/DBAAA.csdl|res://*/DBAAA.ssdl|res://*/DBAAA.msl;provider=System.Data.SqlClient;
provider connection string="
data source=MyMachineDomainHQGWD2S\SQLEXPRESS;
initial catalog=My_DB_TEST;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Hope it can help :D
======================================================================
UPDATE
======================================================================
I've got the problem again! I want to add the strongly note that YOU CANNOT SET data source AS YOU LOCAL IP!! PLEASE SET IT TO $DOMAIN\$MACHINE_NAME
Then you can access the database with locahost:port/service or _your_ip:port/service
======================================================================
This site can’t be reached localhost refused to connect
I found this problem when I started debugging web service on VS2015. Refer to the answer http://stackoverflow.com/a/35418904/6879961, I added a step before following them.
I deleted .vs\config\applicationhost.config and then I create the virtual directory. This action will generate the new applicationhost.config for you with the correct URL.

* this image is from stackoverflow
I deleted .vs\config\applicationhost.config and then I create the virtual directory. This action will generate the new applicationhost.config for you with the correct URL.

* this image is from stackoverflow
สมัครสมาชิก:
บทความ (Atom)
