How do I test wildcard domain logic - c#

Here is an interesting problem... I have configured wildcard domains on my production site and will be using it as a customer identifier. This allows me to display the right banner/css/logos for each customer simply be pulling out the wildcard prefix (eg. CompanyABC.website.com will be displayed the logos and branding related to CompanyABC).
But how do I test this on my local Visual Studio 2010 installation? In production the site loads COMPANY.website.com etc., but locally I can only use the flat http://localhost address.

Hosts file?
127.0.0.1 companyabc.website.local

Put the relevant code in a separate class and create a unit test for it.

Just get one more computer. I would prefer one more laptop. Make the IP dynamic and test it there. Results would give higher and accurate results, since your not testing different IPs on the same computer.

Related

Can I port my C# (SQL Server) app to a different PC?

I have created a project for my college using C# and with SQL Server 2016 and SQL Server 2017 Management Studio.
It is possible that my teacher would want to test it on a different PC, so how could I run it on different PC?
The database I used was made on a local server in my laptop.
Is it possible to port it to another PC?
Sorry if port is a bad word for this but its the best thing I can come up with.
Click here for image
Port is the wrong word, but I do think I understand what you want to do - "move" the application so he can run it on another endpoint.
Generally speaking:
- The other endpoint will need the same version of .Net you're dependent on
- He will need the same version of SQL Server running on his laptop
Step 1: Create a backup of your database
Instructions are here:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/create-a-full-database-backup-sql-server
Step 2: Create an archive (zip/rar, whatever) of Executable
If you haven't compiled it into a standalone EXE and have library dependencies, gather them all (normally in your build directory in your project folder)
Step 3: Move the .bak file and archive to the other computer
Step 4: Restore the database
Instructions are here:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/file-restores-simple-recovery-model
Notes:
As Osman Rahimi noted in the comment, you will need to make sure your connection string in your app isn't hardcoded to your IP or machine name. It is best to use localhost or 127.0.0.1 instead.
An alternative you might want to consider is putting the database IP into a variable you load in the .config file. That way you can concatenate it into your connection string and just instruct your teacher to make the change in the config file.
It could also mean you can leave the database on your workstations if it is on the same network and you have nothing blocking the required SQL Server ports between both endpoints.
The salient code/points of reference for the second point above are as follows:
//import statement to reference library
using System.Configuration;
//reference variable in your code (for your connection-string):
ConfigurationManager.AppSettings["SURVEYPATH_SERVER"].ToString()
//where to add variable in config file
<configuration>
<appSettings>
<add key="SQL_SERVER_IP" value="127.0.0.1" />
</appSettings>
</configuration>
To access to database on another machine
Step 1: both machines should be connected to same local network
Step 2: To share database on different machine please refer the following link
https://learn.microsoft.com/en-us/sql/relational-databases/lesson-2-connecting-from-another-computer
I hope this will help you
Simplest you can access it using IP address of your machine to browse it from different machine. make sure your firewall is open or at least the port 80 is open to be able to browse it from outside.
However if you want to copy all code to a different machine and run it from there you have the following options:
1) Do it manually by copying your code and do you configuration there which might take time and might even error prone especially if you miss some configuration. if you just want to copy code only and leave database in your machine, make sure your firewall is open (at least port 1433)
2) Another way if you are familiar with containerization, you can build and share docker's images and then run containers in the other machine easily.
Some articles:
Overview of .NET and Containers
Introduction to SQL Server Containers
Dockerizing Existing .NET MVC
3) Also you can use vagrant as a different solution to share your box and to download it there or through manual virtualization by developing your application from start (Virtualbox, VMware ..etc)

Request.Url.Host vs Request.Url.Authority

I've inherited an ASP.NET web application written in C#. In many pages throughout the site the hostname is retrieved using:
BaseHost = Request.Url.Host;
Since I am using Visual Studio 2012 Express and it's installed local IIS Express server, I seem to be stuck with a port number appended to the hostname (localhost) when I debug/run locally. The above code does not result in the port number being included and as such breaks links that are generated by code (menu item links, redirects, etc).
I see that I can overcome the issue by changing the code to:
BaseHost = Request.Url.Authority;
This seems to fix it by including the port when I'm running locally (localhost:4652) and when published to my staging server (development.mysite.com).
My question: Is this bad form? Is there a time or a situation in which this is going to cause problems on my live site? It just seems a lot easier to do a quick replace of all these instances. I've considered writing a small routine to append : with Request.Url.Port, but it seems easier just to use Request.Url.Authority. Too easy maybe...
I've tried to research my question online and at MSDN, but I don't see an answer.
According to MSDN Authority includes the port number while Host does not. Another aspect is that Authority will escape reserved characters if need be.
Without knowing your application it is hard to say whether it is a good idea, but in general I would suspect that it won't break anything... so go ahead...
Another option is to run the application IIS instead of IIS Express...
My problem with this is that it ALWAYS adds the port, even when the port is not required. This can cause issues with multiple servers in some cases For example, in a production server environment behind a firewall on a pair of load-balanced web servers, it kept putting the firewall port in place, but that caused the URL to break because the port was tied to a specific web server in the server farm that wouldn't map correctly through the firewall. So I would be very careful with this method if you're using it across multiple servers. It caused a breaking issue with our application and had to be reverted back to using Url.Host. Plus, it made production web URL's look weird with the port number.

Test subdomains on local webserver in VS

I am still experimenting with Azure multi tenant development. I now have my first trial thingy, but in order to use subdomain names (customer.site.com) I need to switch my Azure website to shared/reserved. Since I am still experimenting, I rather not start paying for Azure. Is there a way around this? Or, is it possible to test the multi-tenant part in my local visual studio webserver?
No, you can't have custom domain names with FREE websites.
But what you could do, is to switch the tenant recognition from sub-domain to a path. So instead of having tenant10.site.com/ you would have mysites.azurewebsites.net/tenant10/. That would basically be just a change in URL Rewrite rules - which I think is the right way to handle multi-tenancy recognition at URL level. And URL rewrite is supported in Azure WebSites as well Azure Cloud Services.
Testing the multi-tenancy locally is even easier. You just open your hosts file (in a typical windows installation it is located in c:\windows\system32\drivers\etc\hosts. Just add entries for all (sub)domains you want to test, and map them to 127.0.0.1. Something like:
127.0.0.1 tenant1.mydomain.com
127.0.0.1 tenant2.mydomain.com
127.0.0.1 tenant15.mydomain.com
...
Then, run your project with F5 like you would normally do and manually type in the new address in browser's address bar: tenant1.mydomain.com.
However first launch the project with F5 to check the real IP address of local development fabric, because sometimes it may not be 127.0.0.1, but 127.0.0.8 or something else. The IP address that is used in your browser's initial launch is the IP Address you have to fill in your hosts file.
However, if you work with real (sub)domain names in hosts file, do never forget to remove the entries from it, as you will never reach the real Internet sites.

Get List of Local Network Computer Names

I am using visual studio 2010 and I have searched on the net for help and other people using the DirectoryEntry("WinNT:") but it doesn't seem to work for me. I can see my network workgroups and if I use DirectoryEntry("WinNT://MYWORKGROUP") I can't see any computers listed.
Please help I am not sure why it isn't working for me.
Thanks
Getting computer names from my network places:
Do not use DirectoryServices unless your sure of a domain environment. The System.DirectoryServices class is an ADSI wrapper that dosent work without an Active Directory to query against. NetServerEnum() works on workgroups and domains but dosen't guarantee the most reliable data (not all machines may show up). It relies on the Computer Browser service.
To browse the local Windows network, NetBIOS name resolution must be running and correctly configured. In a corporate network that often means the presence of a WINS server. The required components are not enabled by default on modern Windows installations.
Before trying to do anything from your own code, ensure that the infrastructure is in place. Open Windows Explorer and expand the "Network" node. If name Windows browsing is correctly you should see the list of computers on the network there. If the list is empty, the problem isn't in your code.

Multiple instances of an ASP.net application from a single web site configuration in IIS

As the title suggests I'd like to create a single Web Site in IIS that creates multiple instances of an ASP.net application based on the requested Host.
So that all instances are running the same codebase, but each instance has it's own Application object, Session's collection etc.
For example :
host1.domain.tld/default.aspx -> this.Application["foo"] = "host1"
host2.domain.tld/default.aspx -> this.Application["foo"] = "host2"
host3.domain.tld/default.aspx -> this.Application["foo"] = "host3"
I know I can configure IIS to listen to a specific IP address, and set the DNS for host(1|2|3).domain.tld to point at this IP address. Then use Global.asax to check the requested host to setup host specific settings. But the application will still be running as a single instance on the server.
I'd rather have multiple instances of the application running for each host so that their runtime is fully separated, it would also be nice if I could have them in separate Application Pools too, but that's not so important
Of course, I could add the sites individually into the IIS on the servers, but there are some 1600 instances that will need to be configured and this will be very time consuming to do and difficult to manage.
Being able to setup a single instances on a number of servers, then control the load balancing via DNS configuration or filtering on the firewalls both of which can be easily controlled programmatically.
FYI - the asp.net version in use is 4.0 and the IIS is running on a Windows Server 2008.
Any suggestions would be great.
Many thanks
The simplest and most robust way to do this would be to setup individual IIS sites. I know you don't want to do this because it will be very time consuming and definately would be difficult to manage.
However, you've already created a website so now perhaps it's time to create a management tool for instances of that website. As there are 1600 instances that you want to configure, there's a fairly good chance that you already have details of those 1600 instances stored somewhere, such as a database or a spreadsheet.
So:
Get the data about the 1600 instances into a usable format, a Sql Server database (Express, or paid for!) would probably be ideal.
Investigate the IIS7 provisioning APIs
Put together a tool that allows you to create all 1600 instances from the data you have about them, automatically / in batches, via the IIS7 API.
Maintain tool and expand it ready for the inevitable changes that will be required when you need to add or remove instances.
Don't forget that putting your own tool together for a task such as this gives you a lot of flexibility, although there may be tools out there for this purpose that are worthy of investigation. For that (i.e. a non-programmatic solution), I'd suggest asking at http://www.serverfault.com

Categories